Allow uploading files from eww
[emacs.git] / lisp / net / eww.el
blob306d5dca50786d73e28fbb8d6622968faffdc82e
1 ;;; eww.el --- Emacs Web Wowser
3 ;; Copyright (C) 2013-2014 Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: html
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;;; Code:
27 (eval-when-compile (require 'cl))
28 (require 'format-spec)
29 (require 'shr)
30 (require 'url)
31 (require 'url-queue)
32 (require 'mm-url)
33 (eval-when-compile (require 'subr-x)) ;; for string-trim
35 (defgroup eww nil
36 "Emacs Web Wowser"
37 :version "24.4"
38 :link '(custom-manual "(eww) Top")
39 :group 'hypermedia
40 :prefix "eww-")
42 (defcustom eww-header-line-format "%t: %u"
43 "Header line format.
44 - %t is replaced by the title.
45 - %u is replaced by the URL."
46 :version "24.4"
47 :group 'eww
48 :type 'string)
50 (defcustom eww-search-prefix "https://duckduckgo.com/html/?q="
51 "Prefix URL to search engine"
52 :version "24.4"
53 :group 'eww
54 :type 'string)
56 (defcustom eww-download-directory "~/Downloads/"
57 "Directory where files will downloaded."
58 :version "24.4"
59 :group 'eww
60 :type 'string)
62 (defcustom eww-bookmarks-directory user-emacs-directory
63 "Directory where bookmark files will be stored."
64 :version "25.1"
65 :group 'eww
66 :type 'string)
68 (defcustom eww-use-external-browser-for-content-type
69 "\\`\\(video/\\|audio/\\|application/ogg\\)"
70 "Always use external browser for specified content-type."
71 :version "24.4"
72 :group 'eww
73 :type '(choice (const :tag "Never" nil)
74 regexp))
76 (defcustom eww-form-checkbox-selected-symbol "[X]"
77 "Symbol used to represent a selected checkbox.
78 See also `eww-form-checkbox-symbol'."
79 :version "24.4"
80 :group 'eww
81 :type '(choice (const "[X]")
82 (const "☒") ; Unicode BALLOT BOX WITH X
83 (const "☑") ; Unicode BALLOT BOX WITH CHECK
84 string))
86 (defcustom eww-form-checkbox-symbol "[ ]"
87 "Symbol used to represent a checkbox.
88 See also `eww-form-checkbox-selected-symbol'."
89 :version "24.4"
90 :group 'eww
91 :type '(choice (const "[ ]")
92 (const "☐") ; Unicode BALLOT BOX
93 string))
95 (defface eww-form-submit
96 '((((type x w32 ns) (class color)) ; Like default mode line
97 :box (:line-width 2 :style released-button)
98 :background "#808080" :foreground "black"))
99 "Face for eww buffer buttons."
100 :version "24.4"
101 :group 'eww)
103 (defface eww-form-file
104 '((((type x w32 ns) (class color)) ; Like default mode line
105 :box (:line-width 2 :style released-button)
106 :background "#808080" :foreground "black"))
107 "Face for eww buffer buttons."
108 :version "24.4"
109 :group 'eww
110 :type "Browse")
112 (defface eww-form-checkbox
113 '((((type x w32 ns) (class color)) ; Like default mode line
114 :box (:line-width 2 :style released-button)
115 :background "lightgrey" :foreground "black"))
116 "Face for eww buffer buttons."
117 :version "24.4"
118 :group 'eww)
120 (defface eww-form-select
121 '((((type x w32 ns) (class color)) ; Like default mode line
122 :box (:line-width 2 :style released-button)
123 :background "lightgrey" :foreground "black"))
124 "Face for eww buffer buttons."
125 :version "24.4"
126 :group 'eww)
128 (defface eww-form-text
129 '((t (:background "#505050"
130 :foreground "white"
131 :box (:line-width 1))))
132 "Face for eww text inputs."
133 :version "24.4"
134 :group 'eww)
136 (defface eww-form-textarea
137 '((t (:background "#C0C0C0"
138 :foreground "black"
139 :box (:line-width 1))))
140 "Face for eww textarea inputs."
141 :version "24.4"
142 :group 'eww)
144 (defvar eww-data nil)
145 (defvar eww-history nil)
146 (defvar eww-history-position 0)
148 (defvar eww-local-regex "localhost"
149 "When this regex is found in the URL, it's not a keyword but an address.")
151 (defvar eww-link-keymap
152 (let ((map (copy-keymap shr-map)))
153 (define-key map "\r" 'eww-follow-link)
154 map))
156 ;;;###autoload
157 (defun eww (url)
158 "Fetch URL and render the page.
159 If the input doesn't look like an URL or a domain name, the
160 word(s) will be searched for via `eww-search-prefix'."
161 (interactive "sEnter URL or keywords: ")
162 (setq url (string-trim url))
163 (cond ((string-match-p "\\`file:/" url))
164 ((string-match-p "\\`ftp://" url)
165 (user-error "FTP is not supported."))
167 (if (and (= (length (split-string url)) 1)
168 (or (and (not (string-match-p "\\`[\"\'].*[\"\']\\'" url))
169 (> (length (split-string url "[.:]")) 1))
170 (string-match eww-local-regex url)))
171 (progn
172 (unless (string-match-p "\\`[a-zA-Z][-a-zA-Z0-9+.]*://" url)
173 (setq url (concat "http://" url)))
174 ;; some site don't redirect final /
175 (when (string= (url-filename (url-generic-parse-url url)) "")
176 (setq url (concat url "/"))))
177 (setq url (concat eww-search-prefix
178 (replace-regexp-in-string " " "+" url))))))
179 (url-retrieve url 'eww-render
180 (list url nil
181 (and (eq major-mode 'eww-mode)
182 (current-buffer)))))
184 ;;;###autoload (defalias 'browse-web 'eww)
186 ;;;###autoload
187 (defun eww-open-file (file)
188 "Render a file using EWW."
189 (interactive "fFile: ")
190 (eww (concat "file://"
191 (and (memq system-type '(windows-nt ms-dos))
192 "/")
193 (expand-file-name file))))
195 (defun eww-render (status url &optional point buffer)
196 (let ((redirect (plist-get status :redirect)))
197 (when redirect
198 (setq url redirect)))
199 (let* ((headers (eww-parse-headers))
200 (content-type
201 (mail-header-parse-content-type
202 (or (cdr (assoc "content-type" headers))
203 "text/plain")))
204 (charset (intern
205 (downcase
206 (or (cdr (assq 'charset (cdr content-type)))
207 (eww-detect-charset (equal (car content-type)
208 "text/html"))
209 "utf8"))))
210 (data-buffer (current-buffer)))
211 (unwind-protect
212 (progn
213 (plist-put eww-data :title "")
214 (cond
215 ((and eww-use-external-browser-for-content-type
216 (string-match-p eww-use-external-browser-for-content-type
217 (car content-type)))
218 (eww-browse-with-external-browser url))
219 ((equal (car content-type) "text/html")
220 (eww-display-html charset url nil point buffer))
221 ((equal (car content-type) "application/pdf")
222 (eww-display-pdf))
223 ((string-match-p "\\`image/" (car content-type))
224 (eww-display-image buffer)
225 (eww-update-header-line-format))
227 (eww-display-raw buffer)
228 (eww-update-header-line-format)))
229 (plist-put eww-data :title url)
230 (setq eww-history-position 0))
231 (kill-buffer data-buffer))))
233 (defun eww-parse-headers ()
234 (let ((headers nil))
235 (goto-char (point-min))
236 (while (and (not (eobp))
237 (not (eolp)))
238 (when (looking-at "\\([^:]+\\): *\\(.*\\)")
239 (push (cons (downcase (match-string 1))
240 (match-string 2))
241 headers))
242 (forward-line 1))
243 (unless (eobp)
244 (forward-line 1))
245 headers))
247 (defun eww-detect-charset (html-p)
248 (let ((case-fold-search t)
249 (pt (point)))
250 (or (and html-p
251 (re-search-forward
252 "<meta[\t\n\r ]+[^>]*charset=\"?\\([^\t\n\r \"/>]+\\)[\\\"'.*]" nil t)
253 (goto-char pt)
254 (match-string 1))
255 (and (looking-at
256 "[\t\n\r ]*<\\?xml[\t\n\r ]+[^>]*encoding=\"\\([^\"]+\\)")
257 (match-string 1)))))
259 (declare-function libxml-parse-html-region "xml.c"
260 (start end &optional base-url))
262 (defun eww-display-html (charset url &optional document point buffer)
263 (or (fboundp 'libxml-parse-html-region)
264 (error "This function requires Emacs to be compiled with libxml2"))
265 ;; There should be a better way to abort loading images
266 ;; asynchronously.
267 (setq url-queue nil)
268 (let ((document
269 (or document
270 (list
271 'base (list (cons 'href url))
272 (progn
273 (unless (eq charset 'utf-8)
274 (condition-case nil
275 (decode-coding-region (point) (point-max) charset)
276 (coding-system-error nil)))
277 (libxml-parse-html-region (point) (point-max))))))
278 (source (and (null document)
279 (buffer-substring (point) (point-max)))))
280 (eww-setup-buffer buffer)
281 (plist-put eww-data :source source)
282 (plist-put eww-data :dom document)
283 (let ((inhibit-read-only t)
284 (after-change-functions nil)
285 (shr-target-id (url-target (url-generic-parse-url url)))
286 (shr-external-rendering-functions
287 '((title . eww-tag-title)
288 (form . eww-tag-form)
289 (input . eww-tag-input)
290 (textarea . eww-tag-textarea)
291 (body . eww-tag-body)
292 (select . eww-tag-select)
293 (link . eww-tag-link)
294 (a . eww-tag-a))))
295 (shr-insert-document document)
296 (cond
297 (point
298 (goto-char point))
299 (shr-target-id
300 (goto-char (point-min))
301 (let ((point (next-single-property-change
302 (point-min) 'shr-target-id)))
303 (when point
304 (goto-char point))))
306 (goto-char (point-min))
307 ;; Don't leave point inside forms, because the normal eww
308 ;; commands aren't available there.
309 (while (and (not (eobp))
310 (get-text-property (point) 'eww-form))
311 (forward-line 1)))))
312 (plist-put eww-data :url url)
313 (setq eww-history-position 0)
314 (eww-update-header-line-format)))
316 (defun eww-handle-link (cont)
317 (let* ((rel (assq :rel cont))
318 (href (assq :href cont))
319 (where (assoc
320 ;; The text associated with :rel is case-insensitive.
321 (if rel (downcase (cdr rel)))
322 '(("next" . :next)
323 ;; Texinfo uses "previous", but HTML specifies
324 ;; "prev", so recognize both.
325 ("previous" . :previous)
326 ("prev" . :previous)
327 ;; HTML specifies "start" but also "contents",
328 ;; and Gtk seems to use "home". Recognize
329 ;; them all; but store them in different
330 ;; variables so that we can readily choose the
331 ;; "best" one.
332 ("start" . :start)
333 ("home" . :home)
334 ("contents" . :contents)
335 ("up" . up)))))
336 (and href
337 where
338 (plist-put eww-data (cdr where) (cdr href)))))
340 (defun eww-tag-link (cont)
341 (eww-handle-link cont)
342 (shr-generic cont))
344 (defun eww-tag-a (cont)
345 (eww-handle-link cont)
346 (let ((start (point)))
347 (shr-tag-a cont)
348 (put-text-property start (point) 'keymap eww-link-keymap)))
350 (defun eww-update-header-line-format ()
351 (if eww-header-line-format
352 (setq header-line-format
353 (replace-regexp-in-string
354 "%" "%%"
355 ;; FIXME? Title can be blank. Default to, eg, last component
356 ;; of url?
357 (format-spec eww-header-line-format
358 `((?u . ,(plist-get eww-data :url))
359 (?t . ,(or (plist-get eww-data :title) ""))))))
360 (setq header-line-format nil)))
362 (defun eww-tag-title (cont)
363 (let ((title ""))
364 (dolist (sub cont)
365 (when (eq (car sub) 'text)
366 (setq title (concat title (cdr sub)))))
367 (plist-put eww-data :title
368 (replace-regexp-in-string
369 "^ \\| $" ""
370 (replace-regexp-in-string "[ \t\r\n]+" " " title))))
371 (eww-update-header-line-format))
373 (defun eww-tag-body (cont)
374 (let* ((start (point))
375 (fgcolor (cdr (or (assq :fgcolor cont)
376 (assq :text cont))))
377 (bgcolor (cdr (assq :bgcolor cont)))
378 (shr-stylesheet (list (cons 'color fgcolor)
379 (cons 'background-color bgcolor))))
380 (shr-generic cont)
381 (shr-colorize-region start (point) fgcolor bgcolor)))
383 (defun eww-display-raw (&optional buffer)
384 (let ((data (buffer-substring (point) (point-max))))
385 (eww-setup-buffer buffer)
386 (let ((inhibit-read-only t))
387 (insert data))
388 (goto-char (point-min))))
390 (defun eww-display-image (&optional buffer)
391 (let ((data (shr-parse-image-data)))
392 (eww-setup-buffer buffer)
393 (let ((inhibit-read-only t))
394 (shr-put-image data nil))
395 (goto-char (point-min))))
397 (defun eww-display-pdf ()
398 (let ((data (buffer-substring (point) (point-max))))
399 (switch-to-buffer (get-buffer-create "*eww pdf*"))
400 (let ((coding-system-for-write 'raw-text)
401 (inhibit-read-only t))
402 (erase-buffer)
403 (insert data)
404 (doc-view-mode)))
405 (goto-char (point-min)))
407 (defun eww-setup-buffer (&optional buffer)
408 (switch-to-buffer
409 (if (buffer-live-p buffer)
410 buffer
411 (get-buffer-create "*eww*")))
412 (let ((inhibit-read-only t))
413 (remove-overlays)
414 (erase-buffer))
415 (unless (eq major-mode 'eww-mode)
416 (eww-mode)))
418 (defun eww-view-source ()
419 "View the HTML source code of the current page."
420 (interactive)
421 (let ((buf (get-buffer-create "*eww-source*"))
422 (source (plist-get eww-data :source)))
423 (with-current-buffer buf
424 (delete-region (point-min) (point-max))
425 (insert (or source "no source"))
426 (goto-char (point-min))
427 (when (fboundp 'html-mode)
428 (html-mode)))
429 (view-buffer buf)))
431 (defun eww-readable ()
432 "View the main \"readable\" parts of the current web page.
433 This command uses heuristics to find the parts of the web page that
434 contains the main textual portion, leaving out navigation menus and
435 the like."
436 (interactive)
437 (let* ((old-data eww-data)
438 (dom (shr-transform-dom
439 (with-temp-buffer
440 (insert (plist-get old-data :source))
441 (condition-case nil
442 (decode-coding-region (point-min) (point-max) 'utf-8)
443 (coding-system-error nil))
444 (libxml-parse-html-region (point-min) (point-max))))))
445 (eww-score-readability dom)
446 (eww-save-history)
447 (eww-display-html nil nil
448 (shr-retransform-dom
449 (eww-highest-readability dom))
450 nil (current-buffer))
451 (dolist (elem '(:source :url :title :next :previous :up))
452 (plist-put eww-data elem (plist-get old-data elem)))
453 (eww-update-header-line-format)))
455 (defun eww-score-readability (node)
456 (let ((score -1))
457 (cond
458 ((memq (car node) '(script head comment))
459 (setq score -2))
460 ((eq (car node) 'meta)
461 (setq score -1))
462 ((eq (car node) 'img)
463 (setq score 2))
464 ((eq (car node) 'a)
465 (setq score (- (length (split-string
466 (or (cdr (assoc 'text (cdr node))) ""))))))
468 (dolist (elem (cdr node))
469 (cond
470 ((and (stringp (cdr elem))
471 (eq (car elem) 'text))
472 (setq score (+ score (length (split-string (cdr elem))))))
473 ((consp (cdr elem))
474 (setq score (+ score
475 (or (cdr (assoc :eww-readability-score (cdr elem)))
476 (eww-score-readability elem)))))))))
477 ;; Cache the score of the node to avoid recomputing all the time.
478 (setcdr node (cons (cons :eww-readability-score score) (cdr node)))
479 score))
481 (defun eww-highest-readability (node)
482 (let ((result node)
483 highest)
484 (dolist (elem (cdr node))
485 (when (and (consp (cdr elem))
486 (> (or (cdr (assoc
487 :eww-readability-score
488 (setq highest
489 (eww-highest-readability elem))))
490 most-negative-fixnum)
491 (or (cdr (assoc :eww-readability-score (cdr result)))
492 most-negative-fixnum)))
493 (setq result highest)))
494 result))
496 (defvar eww-mode-map
497 (let ((map (make-sparse-keymap)))
498 (suppress-keymap map)
499 (define-key map "q" 'quit-window)
500 (define-key map "g" 'eww-reload)
501 (define-key map [?\t] 'shr-next-link)
502 (define-key map [?\M-\t] 'shr-previous-link)
503 (define-key map [delete] 'scroll-down-command)
504 (define-key map [?\S-\ ] 'scroll-down-command)
505 (define-key map "\177" 'scroll-down-command)
506 (define-key map " " 'scroll-up-command)
507 (define-key map "l" 'eww-back-url)
508 (define-key map "r" 'eww-forward-url)
509 (define-key map "n" 'eww-next-url)
510 (define-key map "p" 'eww-previous-url)
511 (define-key map "u" 'eww-up-url)
512 (define-key map "t" 'eww-top-url)
513 (define-key map "&" 'eww-browse-with-external-browser)
514 (define-key map "d" 'eww-download)
515 (define-key map "w" 'eww-copy-page-url)
516 (define-key map "C" 'url-cookie-list)
517 (define-key map "v" 'eww-view-source)
518 (define-key map "R" 'eww-readable)
519 (define-key map "H" 'eww-list-histories)
521 (define-key map "b" 'eww-add-bookmark)
522 (define-key map "B" 'eww-list-bookmarks)
523 (define-key map [(meta n)] 'eww-next-bookmark)
524 (define-key map [(meta p)] 'eww-previous-bookmark)
526 (easy-menu-define nil map ""
527 '("Eww"
528 ["Exit" quit-window t]
529 ["Close browser" quit-window t]
530 ["Reload" eww-reload t]
531 ["Back to previous page" eww-back-url
532 :active (not (zerop (length eww-history)))]
533 ["Forward to next page" eww-forward-url
534 :active (not (zerop eww-history-position))]
535 ["Browse with external browser" eww-browse-with-external-browser t]
536 ["Download" eww-download t]
537 ["View page source" eww-view-source]
538 ["Copy page URL" eww-copy-page-url t]
539 ["List histories" eww-list-histories t]
540 ["Add bookmark" eww-add-bookmark t]
541 ["List bookmarks" eww-list-bookmarks t]
542 ["List cookies" url-cookie-list t]))
543 map))
545 (defvar eww-tool-bar-map
546 (let ((map (make-sparse-keymap)))
547 (dolist (tool-bar-item
548 '((quit-window . "close")
549 (eww-reload . "refresh")
550 (eww-back-url . "left-arrow")
551 (eww-forward-url . "right-arrow")
552 (eww-view-source . "show")
553 (eww-copy-page-url . "copy")
554 (eww-add-bookmark . "bookmark_add"))) ;; ...
555 (tool-bar-local-item-from-menu
556 (car tool-bar-item) (cdr tool-bar-item) map eww-mode-map))
557 map)
558 "Tool bar for `eww-mode'.")
560 (define-derived-mode eww-mode nil "eww"
561 "Mode for browsing the web.
563 \\{eww-mode-map}"
564 (setq-local eww-data (list :title ""))
565 (setq-local browse-url-browser-function 'eww-browse-url)
566 (setq-local after-change-functions 'eww-process-text-input)
567 (setq-local eww-history nil)
568 (setq-local eww-history-position 0)
569 (when (boundp 'tool-bar-map)
570 (setq-local tool-bar-map eww-tool-bar-map))
571 (buffer-disable-undo)
572 ;;(setq buffer-read-only t)
575 ;;;###autoload
576 (defun eww-browse-url (url &optional _new-window)
577 (when (and (equal major-mode 'eww-mode)
578 (plist-get eww-data :url))
579 (eww-save-history))
580 (eww url))
582 (defun eww-back-url ()
583 "Go to the previously displayed page."
584 (interactive)
585 (when (>= eww-history-position (length eww-history))
586 (user-error "No previous page"))
587 (eww-save-history)
588 (setq eww-history-position (+ eww-history-position 2))
589 (eww-restore-history (elt eww-history (1- eww-history-position))))
591 (defun eww-forward-url ()
592 "Go to the next displayed page."
593 (interactive)
594 (when (zerop eww-history-position)
595 (user-error "No next page"))
596 (eww-save-history)
597 (eww-restore-history (elt eww-history (1- eww-history-position))))
599 (defun eww-restore-history (elem)
600 (let ((inhibit-read-only t))
601 (erase-buffer)
602 (insert (plist-get elem :text))
603 (goto-char (plist-get elem :point))
604 (setq eww-data elem)
605 (eww-update-header-line-format)))
607 (defun eww-next-url ()
608 "Go to the page marked `next'.
609 A page is marked `next' if rel=\"next\" appears in a <link>
610 or <a> tag."
611 (interactive)
612 (if (plist-get eww-data :next)
613 (eww-browse-url (shr-expand-url (plist-get eww-data :next)
614 (plist-get eww-data :url)))
615 (user-error "No `next' on this page")))
617 (defun eww-previous-url ()
618 "Go to the page marked `previous'.
619 A page is marked `previous' if rel=\"previous\" appears in a <link>
620 or <a> tag."
621 (interactive)
622 (if (plist-get eww-data :previous)
623 (eww-browse-url (shr-expand-url (plist-get eww-data :previous)
624 (plist-get eww-data :url)))
625 (user-error "No `previous' on this page")))
627 (defun eww-up-url ()
628 "Go to the page marked `up'.
629 A page is marked `up' if rel=\"up\" appears in a <link>
630 or <a> tag."
631 (interactive)
632 (if (plist-get eww-data :up)
633 (eww-browse-url (shr-expand-url (plist-get eww-data :up)
634 (plist-get eww-data :url)))
635 (user-error "No `up' on this page")))
637 (defun eww-top-url ()
638 "Go to the page marked `top'.
639 A page is marked `top' if rel=\"start\", rel=\"home\", or rel=\"contents\"
640 appears in a <link> or <a> tag."
641 (interactive)
642 (let ((best-url (or (plist-get eww-data :start)
643 (plist-get eww-data :contents)
644 (plist-get eww-data :home))))
645 (if best-url
646 (eww-browse-url (shr-expand-url best-url (plist-get eww-data :url)))
647 (user-error "No `top' for this page"))))
649 (defun eww-reload ()
650 "Reload the current page."
651 (interactive)
652 (let ((url (plist-get eww-data :url)))
653 (url-retrieve url 'eww-render (list url (point)))))
655 ;; Form support.
657 (defvar eww-form nil)
659 (defvar eww-submit-map
660 (let ((map (make-sparse-keymap)))
661 (define-key map "\r" 'eww-submit)
662 (define-key map [(control c) (control c)] 'eww-submit)
663 map))
665 (defvar eww-submit-file
666 (let ((map (make-sparse-keymap)))
667 (define-key map "\r" 'eww-select-file)
668 (define-key map [(control c) (control c)] 'eww-submit)
669 map))
671 (defvar eww-checkbox-map
672 (let ((map (make-sparse-keymap)))
673 (define-key map " " 'eww-toggle-checkbox)
674 (define-key map "\r" 'eww-toggle-checkbox)
675 (define-key map [(control c) (control c)] 'eww-submit)
676 map))
678 (defvar eww-text-map
679 (let ((map (make-keymap)))
680 (set-keymap-parent map text-mode-map)
681 (define-key map "\r" 'eww-submit)
682 (define-key map [(control a)] 'eww-beginning-of-text)
683 (define-key map [(control c) (control c)] 'eww-submit)
684 (define-key map [(control e)] 'eww-end-of-text)
685 (define-key map [?\t] 'shr-next-link)
686 (define-key map [?\M-\t] 'shr-previous-link)
687 map))
689 (defvar eww-textarea-map
690 (let ((map (make-keymap)))
691 (set-keymap-parent map text-mode-map)
692 (define-key map "\r" 'forward-line)
693 (define-key map [(control c) (control c)] 'eww-submit)
694 (define-key map [?\t] 'shr-next-link)
695 (define-key map [?\M-\t] 'shr-previous-link)
696 map))
698 (defvar eww-select-map
699 (let ((map (make-sparse-keymap)))
700 (define-key map "\r" 'eww-change-select)
701 (define-key map [(control c) (control c)] 'eww-submit)
702 map))
704 (defun eww-beginning-of-text ()
705 "Move to the start of the input field."
706 (interactive)
707 (goto-char (eww-beginning-of-field)))
709 (defun eww-end-of-text ()
710 "Move to the end of the text in the input field."
711 (interactive)
712 (goto-char (eww-end-of-field))
713 (let ((start (eww-beginning-of-field)))
714 (while (and (equal (following-char) ? )
715 (> (point) start))
716 (forward-char -1))
717 (when (> (point) start)
718 (forward-char 1))))
720 (defun eww-beginning-of-field ()
721 (cond
722 ((bobp)
723 (point))
724 ((not (eq (get-text-property (point) 'eww-form)
725 (get-text-property (1- (point)) 'eww-form)))
726 (point))
728 (previous-single-property-change
729 (point) 'eww-form nil (point-min)))))
731 (defun eww-end-of-field ()
732 (1- (next-single-property-change
733 (point) 'eww-form nil (point-max))))
735 (defun eww-tag-form (cont)
736 (let ((eww-form
737 (list (assq :method cont)
738 (assq :action cont)))
739 (start (point)))
740 (shr-ensure-paragraph)
741 (shr-generic cont)
742 (unless (bolp)
743 (insert "\n"))
744 (insert "\n")
745 (when (> (point) start)
746 (put-text-property start (1+ start)
747 'eww-form eww-form))))
749 (defun eww-form-submit (cont)
750 (let ((start (point))
751 (value (cdr (assq :value cont))))
752 (setq value
753 (if (zerop (length value))
754 "Submit"
755 value))
756 (insert value)
757 (add-face-text-property start (point) 'eww-form-submit)
758 (put-text-property start (point) 'eww-form
759 (list :eww-form eww-form
760 :value value
761 :type "submit"
762 :name (cdr (assq :name cont))))
763 (put-text-property start (point) 'keymap eww-submit-map)
764 (insert " ")))
766 (defun eww-form-checkbox (cont)
767 (let ((start (point)))
768 (if (cdr (assq :checked cont))
769 (insert eww-form-checkbox-selected-symbol)
770 (insert eww-form-checkbox-symbol))
771 (add-face-text-property start (point) 'eww-form-checkbox)
772 (put-text-property start (point) 'eww-form
773 (list :eww-form eww-form
774 :value (cdr (assq :value cont))
775 :type (downcase (cdr (assq :type cont)))
776 :checked (cdr (assq :checked cont))
777 :name (cdr (assq :name cont))))
778 (put-text-property start (point) 'keymap eww-checkbox-map)
779 (insert " ")))
781 (defun eww-form-file (cont)
782 (let ((start (point))
783 (value (cdr (assq :value cont))))
784 (setq value
785 (if (zerop (length value))
786 " No file selected"
787 value))
788 (insert "Browse")
789 (add-face-text-property start (point) 'eww-form-file)
790 (insert value)
791 (put-text-property start (point) 'eww-form
792 (list :eww-form eww-form
793 :value (cdr (assq :value cont))
794 :type (downcase (cdr (assq :type cont)))
795 :name (cdr (assq :name cont))))
796 (put-text-property start (point) 'keymap eww-submit-file)
797 (insert " ")))
799 (defun eww-select-file ()
800 "Change the value of the upload file menu under point."
801 (interactive)
802 (let* ((input (get-text-property (point) 'eww-form)))
803 (let ((filename
804 (let ((insert-default-directory t))
805 (read-file-name "filename: "))))
806 (eww-update-field filename (length "Browse"))
807 (plist-put input :filename filename))))
809 (defun eww-form-text (cont)
810 (let ((start (point))
811 (type (downcase (or (cdr (assq :type cont))
812 "text")))
813 (value (or (cdr (assq :value cont)) ""))
814 (width (string-to-number
815 (or (cdr (assq :size cont))
816 "40")))
817 (readonly-property (if (or (cdr (assq :disabled cont))
818 (cdr (assq :readonly cont)))
819 'read-only
820 'inhibit-read-only)))
821 (insert value)
822 (when (< (length value) width)
823 (insert (make-string (- width (length value)) ? )))
824 (put-text-property start (point) 'face 'eww-form-text)
825 (put-text-property start (point) 'local-map eww-text-map)
826 (put-text-property start (point) readonly-property t)
827 (put-text-property start (point) 'eww-form
828 (list :eww-form eww-form
829 :value value
830 :type type
831 :name (cdr (assq :name cont))))
832 (insert " ")))
834 (defconst eww-text-input-types '("text" "password" "textarea"
835 "color" "date" "datetime" "datetime-local"
836 "email" "month" "number" "search" "tel"
837 "time" "url" "week")
838 "List of input types which represent a text input.
839 See URL `https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Input'.")
841 (defun eww-process-text-input (beg end length)
842 (let* ((form (get-text-property (min (1+ end) (point-max)) 'eww-form))
843 (properties (text-properties-at end))
844 (type (plist-get form :type)))
845 (when (and form
846 (member type eww-text-input-types))
847 (cond
848 ((zerop length)
849 ;; Delete some space at the end.
850 (save-excursion
851 (goto-char
852 (if (equal type "textarea")
853 (1- (line-end-position))
854 (eww-end-of-field)))
855 (let ((new (- end beg)))
856 (while (and (> new 0)
857 (eql (following-char) ? ))
858 (delete-region (point) (1+ (point)))
859 (setq new (1- new))))
860 (set-text-properties beg end properties)))
861 ((> length 0)
862 ;; Add padding.
863 (save-excursion
864 (goto-char
865 (if (equal type "textarea")
866 (1- (line-end-position))
867 (eww-end-of-field)))
868 (let ((start (point)))
869 (insert (make-string length ? ))
870 (set-text-properties start (point) properties)))))
871 (let ((value (buffer-substring-no-properties
872 (eww-beginning-of-field)
873 (eww-end-of-field))))
874 (when (string-match " +\\'" value)
875 (setq value (substring value 0 (match-beginning 0))))
876 (plist-put form :value value)
877 (when (equal type "password")
878 ;; Display passwords as asterisks.
879 (let ((start (eww-beginning-of-field)))
880 (put-text-property start (+ start (length value))
881 'display (make-string (length value) ?*))))))))
883 (defun eww-tag-textarea (cont)
884 (let ((start (point))
885 (value (or (cdr (assq :value cont)) ""))
886 (lines (string-to-number
887 (or (cdr (assq :rows cont))
888 "10")))
889 (width (string-to-number
890 (or (cdr (assq :cols cont))
891 "10")))
892 end)
893 (shr-ensure-newline)
894 (insert value)
895 (shr-ensure-newline)
896 (when (< (count-lines start (point)) lines)
897 (dotimes (i (- lines (count-lines start (point))))
898 (insert "\n")))
899 (setq end (point-marker))
900 (goto-char start)
901 (while (< (point) end)
902 (end-of-line)
903 (let ((pad (- width (- (point) (line-beginning-position)))))
904 (when (> pad 0)
905 (insert (make-string pad ? ))))
906 (add-face-text-property (line-beginning-position)
907 (point) 'eww-form-textarea)
908 (put-text-property (line-beginning-position) (point)
909 'local-map eww-textarea-map)
910 (forward-line 1))
911 (put-text-property start (point) 'eww-form
912 (list :eww-form eww-form
913 :value value
914 :type "textarea"
915 :name (cdr (assq :name cont))))))
917 (defun eww-tag-input (cont)
918 (let ((type (downcase (or (cdr (assq :type cont))
919 "text")))
920 (start (point)))
921 (cond
922 ((or (equal type "checkbox")
923 (equal type "radio"))
924 (eww-form-checkbox cont))
925 ((equal type "file")
926 (eww-form-file cont))
927 ((equal type "submit")
928 (eww-form-submit cont))
929 ((equal type "hidden")
930 (let ((form eww-form)
931 (name (cdr (assq :name cont))))
932 ;; Don't add <input type=hidden> elements repeatedly.
933 (while (and form
934 (or (not (consp (car form)))
935 (not (eq (caar form) 'hidden))
936 (not (equal (plist-get (cdr (car form)) :name)
937 name))))
938 (setq form (cdr form)))
939 (unless form
940 (nconc eww-form (list
941 (list 'hidden
942 :name name
943 :value (cdr (assq :value cont))))))))
945 (eww-form-text cont)))
946 (unless (= start (point))
947 (put-text-property start (1+ start) 'help-echo "Input field"))))
949 (defun eww-tag-select (cont)
950 (shr-ensure-paragraph)
951 (let ((menu (list :name (cdr (assq :name cont))
952 :eww-form eww-form))
953 (options nil)
954 (start (point))
955 (max 0)
956 opelem)
957 (if (eq (car (car cont)) 'optgroup)
958 (dolist (groupelem cont)
959 (unless (cdr (assq :disabled (cdr groupelem)))
960 (setq opelem (append opelem (cdr (cdr groupelem))))))
961 (setq opelem cont))
962 (dolist (elem opelem)
963 (when (eq (car elem) 'option)
964 (when (cdr (assq :selected (cdr elem)))
965 (nconc menu (list :value
966 (cdr (assq :value (cdr elem))))))
967 (let ((display (or (cdr (assq 'text (cdr elem))) "")))
968 (setq max (max max (length display)))
969 (push (list 'item
970 :value (cdr (assq :value (cdr elem)))
971 :display display)
972 options))))
973 (when options
974 (setq options (nreverse options))
975 ;; If we have no selected values, default to the first value.
976 (unless (plist-get menu :value)
977 (nconc menu (list :value (nth 2 (car options)))))
978 (nconc menu options)
979 (let ((selected (eww-select-display menu)))
980 (insert selected
981 (make-string (- max (length selected)) ? )))
982 (put-text-property start (point) 'eww-form menu)
983 (add-face-text-property start (point) 'eww-form-select)
984 (put-text-property start (point) 'keymap eww-select-map)
985 (unless (= start (point))
986 (put-text-property start (1+ start) 'help-echo "select field"))
987 (shr-ensure-paragraph))))
989 (defun eww-select-display (select)
990 (let ((value (plist-get select :value))
991 display)
992 (dolist (elem select)
993 (when (and (consp elem)
994 (eq (car elem) 'item)
995 (equal value (plist-get (cdr elem) :value)))
996 (setq display (plist-get (cdr elem) :display))))
997 display))
999 (defun eww-change-select ()
1000 "Change the value of the select drop-down menu under point."
1001 (interactive)
1002 (let* ((input (get-text-property (point) 'eww-form))
1003 (completion-ignore-case t)
1004 (options
1005 (delq nil
1006 (mapcar (lambda (elem)
1007 (and (consp elem)
1008 (eq (car elem) 'item)
1009 (cons (plist-get (cdr elem) :display)
1010 (plist-get (cdr elem) :value))))
1011 input)))
1012 (display
1013 (completing-read "Change value: " options nil 'require-match))
1014 (inhibit-read-only t))
1015 (plist-put input :value (cdr (assoc-string display options t)))
1016 (goto-char
1017 (eww-update-field display))))
1019 (defun eww-update-field (string &optional offset)
1020 (if (not offset) (setq offset 0))
1021 (let ((properties (text-properties-at (point)))
1022 (start (+ (eww-beginning-of-field) offset))
1023 (current-end (1+ (eww-end-of-field)))
1024 (new-end (1+ (+ (eww-beginning-of-field) (length string)))))
1025 (delete-region start current-end)
1026 (forward-char offset)
1027 (insert string
1028 (make-string (- (- (+ new-end offset) start) (length string)) ? ))
1029 (if (= 0 offset) (set-text-properties start new-end properties))
1030 start))
1032 (defun eww-toggle-checkbox ()
1033 "Toggle the value of the checkbox under point."
1034 (interactive)
1035 (let* ((input (get-text-property (point) 'eww-form))
1036 (type (plist-get input :type)))
1037 (if (equal type "checkbox")
1038 (goto-char
1040 (if (plist-get input :checked)
1041 (progn
1042 (plist-put input :checked nil)
1043 (eww-update-field eww-form-checkbox-symbol))
1044 (plist-put input :checked t)
1045 (eww-update-field eww-form-checkbox-selected-symbol))))
1046 ;; Radio button. Switch all other buttons off.
1047 (let ((name (plist-get input :name)))
1048 (save-excursion
1049 (dolist (elem (eww-inputs (plist-get input :eww-form)))
1050 (when (equal (plist-get (cdr elem) :name) name)
1051 (goto-char (car elem))
1052 (if (not (eq (cdr elem) input))
1053 (progn
1054 (plist-put input :checked nil)
1055 (eww-update-field eww-form-checkbox-symbol))
1056 (plist-put input :checked t)
1057 (eww-update-field eww-form-checkbox-selected-symbol)))))
1058 (forward-char 1)))))
1060 (defun eww-inputs (form)
1061 (let ((start (point-min))
1062 (inputs nil))
1063 (while (and start
1064 (< start (point-max)))
1065 (when (or (get-text-property start 'eww-form)
1066 (setq start (next-single-property-change start 'eww-form)))
1067 (when (eq (plist-get (get-text-property start 'eww-form) :eww-form)
1068 form)
1069 (push (cons start (get-text-property start 'eww-form))
1070 inputs))
1071 (setq start (next-single-property-change start 'eww-form))))
1072 (nreverse inputs)))
1074 (defun eww-input-value (input)
1075 (let ((type (plist-get input :type))
1076 (value (plist-get input :value)))
1077 (cond
1078 ((equal type "textarea")
1079 (with-temp-buffer
1080 (insert value)
1081 (goto-char (point-min))
1082 (while (re-search-forward "^ +\n\\| +$" nil t)
1083 (replace-match "" t t))
1084 (buffer-string)))
1086 (if (string-match " +\\'" value)
1087 (substring value 0 (match-beginning 0))
1088 value)))))
1090 (defun eww-submit ()
1091 "Submit the current form."
1092 (interactive)
1093 (let* ((this-input (get-text-property (point) 'eww-form))
1094 (form (plist-get this-input :eww-form))
1095 values next-submit)
1096 (dolist (elem (sort (eww-inputs form)
1097 (lambda (o1 o2)
1098 (< (car o1) (car o2)))))
1099 (let* ((input (cdr elem))
1100 (input-start (car elem))
1101 (name (plist-get input :name)))
1102 (when name
1103 (cond
1104 ((member (plist-get input :type) '("checkbox" "radio"))
1105 (when (plist-get input :checked)
1106 (push (cons name (plist-get input :value))
1107 values)))
1108 ((equal (plist-get input :type) "file")
1109 (push (cons "file"
1110 (list (cons "filedata"
1111 (with-temp-buffer
1112 (insert-file-contents
1113 (plist-get input :filename))
1114 (buffer-string)))
1115 (cons "name" (plist-get input :name))
1116 (cons "filename" (plist-get input :filename))))
1117 values))
1118 ((equal (plist-get input :type) "submit")
1119 ;; We want the values from buttons if we hit a button if
1120 ;; we hit enter on it, or if it's the first button after
1121 ;; the field we did hit return on.
1122 (when (or (eq input this-input)
1123 (and (not (eq input this-input))
1124 (null next-submit)
1125 (> input-start (point))))
1126 (setq next-submit t)
1127 (push (cons name (plist-get input :value))
1128 values)))
1130 (push (cons name (eww-input-value input))
1131 values))))))
1132 (dolist (elem form)
1133 (when (and (consp elem)
1134 (eq (car elem) 'hidden))
1135 (push (cons (plist-get (cdr elem) :name)
1136 (or (plist-get (cdr elem) :value) ""))
1137 values)))
1138 (if (and (stringp (cdr (assq :method form)))
1139 (equal (downcase (cdr (assq :method form))) "post"))
1140 (let ((mtype))
1141 (dolist (x values mtype)
1142 (if (equal (car x) "file")
1143 (progn
1144 (setq mtype "multipart/form-data"))))
1145 (cond ((equal mtype "multipart/form-data")
1146 (let ((boundary (mml-compute-boundary '())))
1147 (let ((url-request-method "POST")
1148 (url-request-extra-headers
1149 (list (cons "Content-Type"
1150 (concat "multipart/form-data; boundary="
1151 boundary))))
1152 (url-request-data
1153 (mm-url-encode-multipart-form-data values boundary)))
1154 (eww-browse-url (shr-expand-url
1155 (cdr (assq :action form))
1156 (plist-get eww-data :url))))))
1158 (let ((url-request-method "POST")
1159 (url-request-extra-headers
1160 '(("Content-Type" .
1161 "application/x-www-form-urlencoded")))
1162 (url-request-data
1163 (mm-url-encode-www-form-urlencoded values)))
1164 (eww-browse-url (shr-expand-url
1165 (cdr (assq :action form))
1166 (plist-get eww-data :url)))))))
1167 (eww-browse-url
1168 (concat
1169 (if (cdr (assq :action form))
1170 (shr-expand-url (cdr (assq :action form))
1171 (plist-get eww-data :url))
1172 (plist-get eww-data :url))
1174 (mm-url-encode-www-form-urlencoded values))))))
1176 (defun eww-browse-with-external-browser (&optional url)
1177 "Browse the current URL with an external browser.
1178 The browser to used is specified by the `shr-external-browser' variable."
1179 (interactive)
1180 (funcall shr-external-browser (or url (plist-get eww-data :url))))
1182 (defun eww-follow-link (&optional external mouse-event)
1183 "Browse the URL under point.
1184 If EXTERNAL, browse the URL using `shr-external-browser'."
1185 (interactive (list current-prefix-arg last-nonmenu-event))
1186 (mouse-set-point mouse-event)
1187 (let ((url (get-text-property (point) 'shr-url)))
1188 (cond
1189 ((not url)
1190 (message "No link under point"))
1191 ((string-match "^mailto:" url)
1192 (browse-url-mail url))
1193 (external
1194 (funcall shr-external-browser url))
1195 ;; This is a #target url in the same page as the current one.
1196 ((and (url-target (url-generic-parse-url url))
1197 (eww-same-page-p url (plist-get eww-data :url)))
1198 (eww-save-history)
1199 (eww-display-html 'utf-8 url (plist-get eww-data :url)
1200 nil (current-buffer)))
1202 (eww-browse-url url)))))
1204 (defun eww-same-page-p (url1 url2)
1205 "Return non-nil if both URLs represent the same page.
1206 Differences in #targets are ignored."
1207 (let ((obj1 (url-generic-parse-url url1))
1208 (obj2 (url-generic-parse-url url2)))
1209 (setf (url-target obj1) nil)
1210 (setf (url-target obj2) nil)
1211 (equal (url-recreate-url obj1) (url-recreate-url obj2))))
1213 (defun eww-copy-page-url ()
1214 (interactive)
1215 (message "%s" (plist-get eww-data :url))
1216 (kill-new (plist-get eww-data :url)))
1218 (defun eww-download ()
1219 "Download URL under point to `eww-download-directory'."
1220 (interactive)
1221 (let ((url (get-text-property (point) 'shr-url)))
1222 (if (not url)
1223 (message "No URL under point")
1224 (url-retrieve url 'eww-download-callback (list url)))))
1226 (defun eww-download-callback (status url)
1227 (unless (plist-get status :error)
1228 (let* ((obj (url-generic-parse-url url))
1229 (path (car (url-path-and-query obj)))
1230 (file (eww-make-unique-file-name (file-name-nondirectory path)
1231 eww-download-directory)))
1232 (goto-char (point-min))
1233 (re-search-forward "\r?\n\r?\n")
1234 (write-region (point) (point-max) file)
1235 (message "Saved %s" file))))
1237 (defun eww-make-unique-file-name (file directory)
1238 (cond
1239 ((zerop (length file))
1240 (setq file "!"))
1241 ((string-match "\\`[.]" file)
1242 (setq file (concat "!" file))))
1243 (let ((count 1))
1244 (while (file-exists-p (expand-file-name file directory))
1245 (setq file
1246 (if (string-match "\\`\\(.*\\)\\([.][^.]+\\)" file)
1247 (format "%s(%d)%s" (match-string 1 file)
1248 count (match-string 2 file))
1249 (format "%s(%d)" file count)))
1250 (setq count (1+ count)))
1251 (expand-file-name file directory)))
1253 ;;; Bookmarks code
1255 (defvar eww-bookmarks nil)
1257 (defun eww-add-bookmark ()
1258 "Add the current page to the bookmarks."
1259 (interactive)
1260 (eww-read-bookmarks)
1261 (dolist (bookmark eww-bookmarks)
1262 (when (equal (plist-get eww-data :url) (plist-get bookmark :url))
1263 (user-error "Already bookmarked")))
1264 (if (y-or-n-p "bookmark this page? ")
1265 (progn
1266 (let ((title (replace-regexp-in-string "[\n\t\r]" " "
1267 (plist-get eww-data :url))))
1268 (setq title (replace-regexp-in-string "\\` +\\| +\\'" "" title))
1269 (push (list :url (plist-get eww-data :url)
1270 :title title
1271 :time (current-time-string))
1272 eww-bookmarks))
1273 (eww-write-bookmarks)
1274 (message "Bookmarked %s (%s)" (plist-get eww-data :url)
1275 (plist-get eww-data :title)))))
1277 (defun eww-write-bookmarks ()
1278 (with-temp-file (expand-file-name "eww-bookmarks" eww-bookmarks-directory)
1279 (insert ";; Auto-generated file; don't edit\n")
1280 (pp eww-bookmarks (current-buffer))))
1282 (defun eww-read-bookmarks ()
1283 (let ((file (expand-file-name "eww-bookmarks" eww-bookmarks-directory)))
1284 (setq eww-bookmarks
1285 (unless (zerop (or (nth 7 (file-attributes file)) 0))
1286 (with-temp-buffer
1287 (insert-file-contents file)
1288 (read (current-buffer)))))))
1290 ;;;###autoload
1291 (defun eww-list-bookmarks ()
1292 "Display the bookmarks."
1293 (interactive)
1294 (eww-bookmark-prepare)
1295 (pop-to-buffer "*eww bookmarks*"))
1297 (defun eww-bookmark-prepare ()
1298 (eww-read-bookmarks)
1299 (unless eww-bookmarks
1300 (user-error "No bookmarks are defined"))
1301 (set-buffer (get-buffer-create "*eww bookmarks*"))
1302 (eww-bookmark-mode)
1303 (let ((format "%-40s %s")
1304 (inhibit-read-only t)
1305 start url)
1306 (erase-buffer)
1307 (setq header-line-format (concat " " (format format "URL" "Title")))
1308 (dolist (bookmark eww-bookmarks)
1309 (setq start (point))
1310 (setq url (plist-get bookmark :url))
1311 (when (> (length url) 40)
1312 (setq url (substring url 0 40)))
1313 (insert (format format url
1314 (plist-get bookmark :title))
1315 "\n")
1316 (put-text-property start (1+ start) 'eww-bookmark bookmark))
1317 (goto-char (point-min))))
1319 (defvar eww-bookmark-kill-ring nil)
1321 (defun eww-bookmark-kill ()
1322 "Kill the current bookmark."
1323 (interactive)
1324 (let* ((start (line-beginning-position))
1325 (bookmark (get-text-property start 'eww-bookmark))
1326 (inhibit-read-only t))
1327 (unless bookmark
1328 (user-error "No bookmark on the current line"))
1329 (forward-line 1)
1330 (push (buffer-substring start (point)) eww-bookmark-kill-ring)
1331 (delete-region start (point))
1332 (setq eww-bookmarks (delq bookmark eww-bookmarks))
1333 (eww-write-bookmarks)))
1335 (defun eww-bookmark-yank ()
1336 "Yank a previously killed bookmark to the current line."
1337 (interactive)
1338 (unless eww-bookmark-kill-ring
1339 (user-error "No previously killed bookmark"))
1340 (beginning-of-line)
1341 (let ((inhibit-read-only t)
1342 (start (point))
1343 bookmark)
1344 (insert (pop eww-bookmark-kill-ring))
1345 (setq bookmark (get-text-property start 'eww-bookmark))
1346 (if (= start (point-min))
1347 (push bookmark eww-bookmarks)
1348 (let ((line (count-lines start (point))))
1349 (setcdr (nthcdr (1- line) eww-bookmarks)
1350 (cons bookmark (nthcdr line eww-bookmarks)))))
1351 (eww-write-bookmarks)))
1353 (defun eww-bookmark-browse ()
1354 "Browse the bookmark under point in eww."
1355 (interactive)
1356 (let ((bookmark (get-text-property (line-beginning-position) 'eww-bookmark)))
1357 (unless bookmark
1358 (user-error "No bookmark on the current line"))
1359 (quit-window)
1360 (eww-browse-url (plist-get bookmark :url))))
1362 (defun eww-next-bookmark ()
1363 "Go to the next bookmark in the list."
1364 (interactive)
1365 (let ((first nil)
1366 bookmark)
1367 (unless (get-buffer "*eww bookmarks*")
1368 (setq first t)
1369 (eww-bookmark-prepare))
1370 (with-current-buffer (get-buffer "*eww bookmarks*")
1371 (when (and (not first)
1372 (not (eobp)))
1373 (forward-line 1))
1374 (setq bookmark (get-text-property (line-beginning-position)
1375 'eww-bookmark))
1376 (unless bookmark
1377 (user-error "No next bookmark")))
1378 (eww-browse-url (plist-get bookmark :url))))
1380 (defun eww-previous-bookmark ()
1381 "Go to the previous bookmark in the list."
1382 (interactive)
1383 (let ((first nil)
1384 bookmark)
1385 (unless (get-buffer "*eww bookmarks*")
1386 (setq first t)
1387 (eww-bookmark-prepare))
1388 (with-current-buffer (get-buffer "*eww bookmarks*")
1389 (if first
1390 (goto-char (point-max))
1391 (beginning-of-line))
1392 ;; On the final line.
1393 (when (eolp)
1394 (forward-line -1))
1395 (if (bobp)
1396 (user-error "No previous bookmark")
1397 (forward-line -1))
1398 (setq bookmark (get-text-property (line-beginning-position)
1399 'eww-bookmark)))
1400 (eww-browse-url (plist-get bookmark :url))))
1402 (defvar eww-bookmark-mode-map
1403 (let ((map (make-sparse-keymap)))
1404 (suppress-keymap map)
1405 (define-key map "q" 'quit-window)
1406 (define-key map [(control k)] 'eww-bookmark-kill)
1407 (define-key map [(control y)] 'eww-bookmark-yank)
1408 (define-key map "\r" 'eww-bookmark-browse)
1410 (easy-menu-define nil map
1411 "Menu for `eww-bookmark-mode-map'."
1412 '("Eww Bookmark"
1413 ["Exit" quit-window t]
1414 ["Browse" eww-bookmark-browse
1415 :active (get-text-property (line-beginning-position) 'eww-bookmark)]
1416 ["Kill" eww-bookmark-kill
1417 :active (get-text-property (line-beginning-position) 'eww-bookmark)]
1418 ["Yank" eww-bookmark-yank
1419 :active eww-bookmark-kill-ring]))
1420 map))
1422 (define-derived-mode eww-bookmark-mode nil "eww bookmarks"
1423 "Mode for listing bookmarks.
1425 \\{eww-bookmark-mode-map}"
1426 (buffer-disable-undo)
1427 (setq buffer-read-only t
1428 truncate-lines t))
1430 ;;; History code
1432 (defun eww-save-history ()
1433 (plist-put eww-data :point (point))
1434 (plist-put eww-data :text (buffer-string))
1435 (push eww-data eww-history)
1436 (setq eww-data (list :title ""))
1437 ;; Don't let the history grow infinitely. We store quite a lot of
1438 ;; data per page.
1439 (when-let (tail (nthcdr 50 eww-history))
1440 (setcdr tail nil)))
1442 (defun eww-list-histories ()
1443 "List the eww-histories."
1444 (interactive)
1445 (when (null eww-history)
1446 (error "No eww-histories are defined"))
1447 (let ((eww-history-trans eww-history))
1448 (set-buffer (get-buffer-create "*eww history*"))
1449 (eww-history-mode)
1450 (let ((inhibit-read-only t)
1451 (domain-length 0)
1452 (title-length 0)
1453 url title format start)
1454 (erase-buffer)
1455 (dolist (history eww-history-trans)
1456 (setq start (point))
1457 (setq domain-length (max domain-length (length (plist-get history :url))))
1458 (setq title-length (max title-length (length (plist-get history :title)))))
1459 (setq format (format "%%-%ds %%-%ds" title-length domain-length)
1460 header-line-format
1461 (concat " " (format format "Title" "URL")))
1462 (dolist (history eww-history-trans)
1463 (setq start (point))
1464 (setq url (plist-get history :url))
1465 (setq title (plist-get history :title))
1466 (insert (format format title url))
1467 (insert "\n")
1468 (put-text-property start (1+ start) 'eww-history history))
1469 (goto-char (point-min)))
1470 (pop-to-buffer "*eww history*")))
1472 (defun eww-history-browse ()
1473 "Browse the history under point in eww."
1474 (interactive)
1475 (let ((history (get-text-property (line-beginning-position) 'eww-history)))
1476 (unless history
1477 (error "No history on the current line"))
1478 (quit-window)
1479 (eww-restore-history history)))
1481 (defvar eww-history-mode-map
1482 (let ((map (make-sparse-keymap)))
1483 (suppress-keymap map)
1484 (define-key map "q" 'quit-window)
1485 (define-key map "\r" 'eww-history-browse)
1486 ;; (define-key map "n" 'next-error-no-select)
1487 ;; (define-key map "p" 'previous-error-no-select)
1489 (easy-menu-define nil map
1490 "Menu for `eww-history-mode-map'."
1491 '("Eww History"
1492 ["Exit" quit-window t]
1493 ["Browse" eww-history-browse
1494 :active (get-text-property (line-beginning-position) 'eww-history)]))
1495 map))
1497 (define-derived-mode eww-history-mode nil "eww history"
1498 "Mode for listing eww-histories.
1500 \\{eww-history-mode-map}"
1501 (buffer-disable-undo)
1502 (setq buffer-read-only t
1503 truncate-lines t))
1505 (provide 'eww)
1507 ;;; eww.el ends here