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