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