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