eww: exit and close actions
[bpt/emacs.git] / lisp / net / eww.el
1 ;;; eww.el --- Emacs Web Wowser
2
3 ;; Copyright (C) 2013 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 'mm-url)
32
33 (defgroup eww nil
34 "Emacs Web Wowser"
35 :version "24.4"
36 :group 'hypermedia
37 :prefix "eww-")
38
39 (defcustom eww-header-line-format "%t: %u"
40 "Header line format.
41 - %t is replaced by the title.
42 - %u is replaced by the URL."
43 :version "24.4"
44 :group 'eww
45 :type 'string)
46
47 (defcustom eww-search-prefix "https://duckduckgo.com/html/?q="
48 "Prefix URL to search engine"
49 :version "24.4"
50 :group 'eww
51 :type 'string)
52
53 (defcustom eww-download-path "~/Downloads/"
54 "Path where files will downloaded."
55 :version "24.4"
56 :group 'eww
57 :type 'string)
58
59 (defcustom eww-use-external-browser-for-content-type
60 "\\`\\(video/\\|audio/\\|application/ogg\\)"
61 "Always use external browser for specified content-type."
62 :version "24.4"
63 :group 'eww
64 :type '(choice (const :tag "Never" nil)
65 regexp))
66
67 (defcustom eww-form-checkbox-selected-symbol "[X]"
68 "Symbol used to represent a selected checkbox.
69 See also `eww-form-checkbox-symbol'."
70 :version "24.4"
71 :group 'eww
72 :type '(choice (const "[X]")
73 (const "☒") ; Unicode BALLOT BOX WITH X
74 (const "☑") ; Unicode BALLOT BOX WITH CHECK
75 string))
76
77 (defcustom eww-form-checkbox-symbol "[ ]"
78 "Symbol used to represent a checkbox.
79 See also `eww-form-checkbox-selected-symbol'."
80 :version "24.4"
81 :group 'eww
82 :type '(choice (const "[ ]")
83 (const "☐") ; Unicode BALLOT BOX
84 string))
85
86 (defface eww-form-submit
87 '((((type x w32 ns) (class color)) ; Like default mode line
88 :box (:line-width 2 :style released-button)
89 :background "#808080" :foreground "black"))
90 "Face for eww buffer buttons."
91 :version "24.4"
92 :group 'eww)
93
94 (defface eww-form-checkbox
95 '((((type x w32 ns) (class color)) ; Like default mode line
96 :box (:line-width 2 :style released-button)
97 :background "lightgrey" :foreground "black"))
98 "Face for eww buffer buttons."
99 :version "24.4"
100 :group 'eww)
101
102 (defface eww-form-select
103 '((((type x w32 ns) (class color)) ; Like default mode line
104 :box (:line-width 2 :style released-button)
105 :background "lightgrey" :foreground "black"))
106 "Face for eww buffer buttons."
107 :version "24.4"
108 :group 'eww)
109
110 (defface eww-form-text
111 '((t (:background "#505050"
112 :foreground "white"
113 :box (:line-width 1))))
114 "Face for eww text inputs."
115 :version "24.4"
116 :group 'eww)
117
118 (defvar eww-current-url nil)
119 (defvar eww-current-dom nil)
120 (defvar eww-current-source nil)
121 (defvar eww-current-title ""
122 "Title of current page.")
123 (defvar eww-history nil)
124 (defvar eww-history-position 0)
125
126 (defvar eww-next-url nil)
127 (defvar eww-previous-url nil)
128 (defvar eww-up-url nil)
129 (defvar eww-home-url nil)
130 (defvar eww-start-url nil)
131 (defvar eww-contents-url nil)
132
133 (defvar eww-local-regex "localhost"
134 "When this regex is found in the URL, it's not a keyword but an address.")
135
136 (defvar eww-link-keymap
137 (let ((map (copy-keymap shr-map)))
138 (define-key map "\r" 'eww-follow-link)
139 map))
140
141 ;;;###autoload
142 (defun eww (url)
143 "Fetch URL and render the page.
144 If the input doesn't look like an URL or a domain name, the
145 word(s) will be searched for via `eww-search-prefix'."
146 (interactive "sEnter URL or keywords: ")
147 (cond ((string-match-p "\\`file:" url))
148 (t
149 (if (and (= (length (split-string url)) 1)
150 (or (> (length (split-string url "\\.")) 1)
151 (string-match eww-local-regex url)))
152 (progn
153 (unless (string-match-p "\\`[a-zA-Z][-a-zA-Z0-9+.]*://" url)
154 (setq url (concat "http://" url)))
155 ;; some site don't redirect final /
156 (when (string= (url-filename (url-generic-parse-url url)) "")
157 (setq url (concat url "/"))))
158 (setq url (concat eww-search-prefix
159 (replace-regexp-in-string " " "+" url))))))
160 (url-retrieve url 'eww-render (list url)))
161
162 ;;;###autoload
163 (defun eww-open-file (file)
164 "Render a file using EWW."
165 (interactive "fFile: ")
166 (eww (concat "file://" (expand-file-name file))))
167
168 (defun eww-render (status url &optional point)
169 (let ((redirect (plist-get status :redirect)))
170 (when redirect
171 (setq url redirect)))
172 (set (make-local-variable 'eww-next-url) nil)
173 (set (make-local-variable 'eww-previous-url) nil)
174 (set (make-local-variable 'eww-up-url) nil)
175 (set (make-local-variable 'eww-home-url) nil)
176 (set (make-local-variable 'eww-start-url) nil)
177 (set (make-local-variable 'eww-contents-url) nil)
178 (let* ((headers (eww-parse-headers))
179 (content-type
180 (mail-header-parse-content-type
181 (or (cdr (assoc "content-type" headers))
182 "text/plain")))
183 (charset (intern
184 (downcase
185 (or (cdr (assq 'charset (cdr content-type)))
186 (eww-detect-charset (equal (car content-type)
187 "text/html"))
188 "utf8"))))
189 (data-buffer (current-buffer)))
190 (unwind-protect
191 (progn
192 (setq eww-current-title "")
193 (cond
194 ((and eww-use-external-browser-for-content-type
195 (string-match-p eww-use-external-browser-for-content-type
196 (car content-type)))
197 (eww-browse-with-external-browser url))
198 ((equal (car content-type) "text/html")
199 (eww-display-html charset url nil point))
200 ((string-match-p "\\`image/" (car content-type))
201 (eww-display-image)
202 (eww-update-header-line-format))
203 (t
204 (eww-display-raw)
205 (eww-update-header-line-format)))
206 (setq eww-current-url url
207 eww-history-position 0))
208 (kill-buffer data-buffer))))
209
210 (defun eww-parse-headers ()
211 (let ((headers nil))
212 (goto-char (point-min))
213 (while (and (not (eobp))
214 (not (eolp)))
215 (when (looking-at "\\([^:]+\\): *\\(.*\\)")
216 (push (cons (downcase (match-string 1))
217 (match-string 2))
218 headers))
219 (forward-line 1))
220 (unless (eobp)
221 (forward-line 1))
222 headers))
223
224 (defun eww-detect-charset (html-p)
225 (let ((case-fold-search t)
226 (pt (point)))
227 (or (and html-p
228 (re-search-forward
229 "<meta[\t\n\r ]+[^>]*charset=\"?\\([^\t\n\r \"/>]+\\)[\\\"'.*]" nil t)
230 (goto-char pt)
231 (match-string 1))
232 (and (looking-at
233 "[\t\n\r ]*<\\?xml[\t\n\r ]+[^>]*encoding=\"\\([^\"]+\\)")
234 (match-string 1)))))
235
236 (declare-function libxml-parse-html-region "xml.c"
237 (start end &optional base-url))
238
239 (defun eww-display-html (charset url &optional document point)
240 (or (fboundp 'libxml-parse-html-region)
241 (error "This function requires Emacs to be compiled with libxml2"))
242 (unless (eq charset 'utf8)
243 (condition-case nil
244 (decode-coding-region (point) (point-max) charset)
245 (coding-system-error nil)))
246 (let ((document
247 (or document
248 (list
249 'base (list (cons 'href url))
250 (libxml-parse-html-region (point) (point-max))))))
251 (setq eww-current-source (buffer-substring (point) (point-max)))
252 (eww-setup-buffer)
253 (setq eww-current-dom document)
254 (let ((inhibit-read-only t)
255 (after-change-functions nil)
256 (shr-width nil)
257 (shr-target-id (url-target (url-generic-parse-url url)))
258 (shr-external-rendering-functions
259 '((title . eww-tag-title)
260 (form . eww-tag-form)
261 (input . eww-tag-input)
262 (textarea . eww-tag-textarea)
263 (body . eww-tag-body)
264 (select . eww-tag-select)
265 (link . eww-tag-link)
266 (a . eww-tag-a))))
267 (shr-insert-document document)
268 (cond
269 (point
270 (goto-char point))
271 (shr-target-id
272 (let ((point (next-single-property-change
273 (point-min) 'shr-target-id)))
274 (when point
275 (goto-char (1+ point)))))
276 (t
277 (goto-char (point-min)))))
278 (setq eww-current-url url
279 eww-history-position 0)
280 (eww-update-header-line-format)))
281
282 (defun eww-handle-link (cont)
283 (let* ((rel (assq :rel cont))
284 (href (assq :href cont))
285 (where (assoc
286 ;; The text associated with :rel is case-insensitive.
287 (if rel (downcase (cdr rel)))
288 '(("next" . eww-next-url)
289 ;; Texinfo uses "previous", but HTML specifies
290 ;; "prev", so recognize both.
291 ("previous" . eww-previous-url)
292 ("prev" . eww-previous-url)
293 ;; HTML specifies "start" but also "contents",
294 ;; and Gtk seems to use "home". Recognize
295 ;; them all; but store them in different
296 ;; variables so that we can readily choose the
297 ;; "best" one.
298 ("start" . eww-start-url)
299 ("home" . eww-home-url)
300 ("contents" . eww-contents-url)
301 ("up" . eww-up-url)))))
302 (and href
303 where
304 (set (cdr where) (cdr href)))))
305
306 (defun eww-tag-link (cont)
307 (eww-handle-link cont)
308 (shr-generic cont))
309
310 (defun eww-tag-a (cont)
311 (eww-handle-link cont)
312 (let ((start (point)))
313 (shr-tag-a cont)
314 (put-text-property start (point) 'keymap eww-link-keymap)))
315
316 (defun eww-update-header-line-format ()
317 (if eww-header-line-format
318 (setq header-line-format
319 (replace-regexp-in-string
320 "%" "%%"
321 ;; FIXME? Title can be blank. Default to, eg, last component
322 ;; of url?
323 (format-spec eww-header-line-format
324 `((?u . ,eww-current-url)
325 (?t . ,eww-current-title)))))
326 (setq header-line-format nil)))
327
328 (defun eww-tag-title (cont)
329 (setq eww-current-title "")
330 (dolist (sub cont)
331 (when (eq (car sub) 'text)
332 (setq eww-current-title (concat eww-current-title (cdr sub)))))
333 (eww-update-header-line-format))
334
335 (defun eww-tag-body (cont)
336 (let* ((start (point))
337 (fgcolor (cdr (or (assq :fgcolor cont)
338 (assq :text cont))))
339 (bgcolor (cdr (assq :bgcolor cont)))
340 (shr-stylesheet (list (cons 'color fgcolor)
341 (cons 'background-color bgcolor))))
342 (shr-generic cont)
343 (eww-colorize-region start (point) fgcolor bgcolor)))
344
345 (defun eww-colorize-region (start end fg &optional bg)
346 (when (or fg bg)
347 (let ((new-colors (shr-color-check fg bg)))
348 (when new-colors
349 (when fg
350 (add-face-text-property start end
351 (list :foreground (cadr new-colors))
352 t))
353 (when bg
354 (add-face-text-property start end
355 (list :background (car new-colors))
356 t))))))
357
358 (defun eww-display-raw ()
359 (let ((data (buffer-substring (point) (point-max))))
360 (eww-setup-buffer)
361 (let ((inhibit-read-only t))
362 (insert data))
363 (goto-char (point-min))))
364
365 (defun eww-display-image ()
366 (let ((data (shr-parse-image-data)))
367 (eww-setup-buffer)
368 (let ((inhibit-read-only t))
369 (shr-put-image data nil))
370 (goto-char (point-min))))
371
372 (defun eww-setup-buffer ()
373 (switch-to-buffer (get-buffer-create "*eww*"))
374 (let ((inhibit-read-only t))
375 (remove-overlays)
376 (erase-buffer))
377 (unless (eq major-mode 'eww-mode)
378 (eww-mode)))
379
380 (defun eww-view-source ()
381 (interactive)
382 (let ((buf (get-buffer-create "*eww-source*"))
383 (source eww-current-source))
384 (with-current-buffer buf
385 (delete-region (point-min) (point-max))
386 (insert (or eww-current-source "no source"))
387 (goto-char (point-min))
388 (when (featurep 'html-mode)
389 (html-mode)))
390 (view-buffer buf)))
391
392 (defvar eww-mode-map
393 (let ((map (make-sparse-keymap)))
394 (suppress-keymap map)
395 (define-key map "q" 'eww-close)
396 (define-key map "Q" 'eww-exit)
397 (define-key map "g" 'eww-reload)
398 (define-key map [tab] 'shr-next-link)
399 (define-key map [backtab] 'shr-previous-link)
400 (define-key map [delete] 'scroll-down-command)
401 (define-key map "\177" 'scroll-down-command)
402 (define-key map " " 'scroll-up-command)
403 (define-key map "l" 'eww-back-url)
404 (define-key map "f" 'eww-forward-url)
405 (define-key map "n" 'eww-next-url)
406 (define-key map "p" 'eww-previous-url)
407 (define-key map "u" 'eww-up-url)
408 (define-key map "t" 'eww-top-url)
409 (define-key map "&" 'eww-browse-with-external-browser)
410 (define-key map "d" 'eww-download)
411 (define-key map "w" 'eww-copy-page-url)
412 (define-key map "C" 'url-cookie-list)
413 (define-key map "v" 'eww-view-source)
414
415 (define-key map "b" 'eww-add-bookmark)
416 (define-key map "B" 'eww-list-bookmarks)
417 (define-key map [(meta n)] 'eww-next-bookmark)
418 (define-key map [(meta p)] 'eww-previous-bookmark)
419
420 (easy-menu-define nil map ""
421 '("Eww"
422 ["Exit" eww-exit t]
423 ["Close browser" eww-close t]
424 ["Reload" eww-reload t]
425 ["Back to previous page" eww-back-url
426 :active (not (zerop (length eww-history)))]
427 ["Forward to next page" eww-forward-url
428 :active (not (zerop eww-history-position))]
429 ["Browse with external browser" eww-browse-with-external-browser t]
430 ["Download" eww-download t]
431 ["View page source" eww-view-source]
432 ["Copy page URL" eww-copy-page-url t]
433 ["Add bookmark" eww-add-bookmark t]
434 ["List bookmarks" eww-list-bookmarks t]
435 ["List cookies" url-cookie-list t]))
436 map))
437
438 (define-derived-mode eww-mode nil "eww"
439 "Mode for browsing the web.
440
441 \\{eww-mode-map}"
442 ;; FIXME? This seems a strange default.
443 (set (make-local-variable 'eww-current-url) 'author)
444 (set (make-local-variable 'eww-current-dom) nil)
445 (set (make-local-variable 'eww-current-source) nil)
446 (set (make-local-variable 'browse-url-browser-function) 'eww-browse-url)
447 (set (make-local-variable 'after-change-functions) 'eww-process-text-input)
448 (set (make-local-variable 'eww-history) nil)
449 (set (make-local-variable 'eww-history-position) 0)
450 (buffer-disable-undo)
451 ;;(setq buffer-read-only t)
452 )
453
454 (defun eww-exit ()
455 "Exit the Emacs Web Wowser."
456 (interactive)
457 (setq eww-history nil)
458 (kill-buffer (current-buffer)))
459
460 (defun eww-close ()
461 "Close the Emacs Web Wowser browser, leaving history intact."
462 (interactive)
463 (quit-window))
464
465 (defun eww-save-history ()
466 (push (list :url eww-current-url
467 :title eww-current-title
468 :point (point)
469 :dom eww-current-dom
470 :source eww-current-source
471 :text (buffer-string))
472 eww-history))
473
474 ;;;###autoload
475 (defun eww-browse-url (url &optional _new-window)
476 (when (and (equal major-mode 'eww-mode)
477 eww-current-url)
478 (eww-save-history))
479 (eww url))
480
481 (defun eww-back-url ()
482 "Go to the previously displayed page."
483 (interactive)
484 (when (>= eww-history-position (length eww-history))
485 (error "No previous page"))
486 (eww-save-history)
487 (setq eww-history-position (+ eww-history-position 2))
488 (eww-restore-history (elt eww-history (1- eww-history-position))))
489
490 (defun eww-forward-url ()
491 "Go to the next displayed page."
492 (interactive)
493 (when (zerop eww-history-position)
494 (error "No next page"))
495 (eww-save-history)
496 (eww-restore-history (elt eww-history (1- eww-history-position))))
497
498 (defun eww-restore-history (elem)
499 (let ((inhibit-read-only t))
500 (erase-buffer)
501 (insert (plist-get elem :text))
502 (setq eww-current-source (plist-get elem :source))
503 (setq eww-current-dom (plist-get elem :dom))
504 (goto-char (plist-get elem :point))
505 (setq eww-current-url (plist-get elem :url)
506 eww-current-title (plist-get elem :title))
507 (eww-update-header-line-format)))
508
509 (defun eww-next-url ()
510 "Go to the page marked `next'.
511 A page is marked `next' if rel=\"next\" appears in a <link>
512 or <a> tag."
513 (interactive)
514 (if eww-next-url
515 (eww-browse-url (shr-expand-url eww-next-url eww-current-url))
516 (error "No `next' on this page")))
517
518 (defun eww-previous-url ()
519 "Go to the page marked `previous'.
520 A page is marked `previous' if rel=\"previous\" appears in a <link>
521 or <a> tag."
522 (interactive)
523 (if eww-previous-url
524 (eww-browse-url (shr-expand-url eww-previous-url eww-current-url))
525 (error "No `previous' on this page")))
526
527 (defun eww-up-url ()
528 "Go to the page marked `up'.
529 A page is marked `up' if rel=\"up\" appears in a <link>
530 or <a> tag."
531 (interactive)
532 (if eww-up-url
533 (eww-browse-url (shr-expand-url eww-up-url eww-current-url))
534 (error "No `up' on this page")))
535
536 (defun eww-top-url ()
537 "Go to the page marked `top'.
538 A page is marked `top' if rel=\"start\", rel=\"home\", or rel=\"contents\"
539 appears in a <link> or <a> tag."
540 (interactive)
541 (let ((best-url (or eww-start-url
542 eww-contents-url
543 eww-home-url)))
544 (if best-url
545 (eww-browse-url (shr-expand-url best-url eww-current-url))
546 (error "No `top' for this page"))))
547
548 (defun eww-reload ()
549 "Reload the current page."
550 (interactive)
551 (url-retrieve eww-current-url 'eww-render
552 (list eww-current-url (point))))
553
554 ;; Form support.
555
556 (defvar eww-form nil)
557
558 (defvar eww-submit-map
559 (let ((map (make-sparse-keymap)))
560 (define-key map "\r" 'eww-submit)
561 (define-key map [(control c) (control c)] 'eww-submit)
562 map))
563
564 (defvar eww-checkbox-map
565 (let ((map (make-sparse-keymap)))
566 (define-key map [space] 'eww-toggle-checkbox)
567 (define-key map "\r" 'eww-toggle-checkbox)
568 (define-key map [(control c) (control c)] 'eww-submit)
569 map))
570
571 (defvar eww-text-map
572 (let ((map (make-keymap)))
573 (set-keymap-parent map text-mode-map)
574 (define-key map "\r" 'eww-submit)
575 (define-key map [(control a)] 'eww-beginning-of-text)
576 (define-key map [(control c) (control c)] 'eww-submit)
577 (define-key map [(control e)] 'eww-end-of-text)
578 (define-key map [tab] 'shr-next-link)
579 (define-key map [backtab] 'shr-previous-link)
580 map))
581
582 (defvar eww-textarea-map
583 (let ((map (make-keymap)))
584 (set-keymap-parent map text-mode-map)
585 (define-key map "\r" 'forward-line)
586 (define-key map [(control c) (control c)] 'eww-submit)
587 (define-key map [tab] 'shr-next-link)
588 (define-key map [backtab] 'shr-previous-link)
589 map))
590
591 (defvar eww-select-map
592 (let ((map (make-sparse-keymap)))
593 (define-key map "\r" 'eww-change-select)
594 (define-key map [(control c) (control c)] 'eww-submit)
595 map))
596
597 (defun eww-beginning-of-text ()
598 "Move to the start of the input field."
599 (interactive)
600 (goto-char (eww-beginning-of-field)))
601
602 (defun eww-end-of-text ()
603 "Move to the end of the text in the input field."
604 (interactive)
605 (goto-char (eww-end-of-field))
606 (let ((start (eww-beginning-of-field)))
607 (while (and (equal (following-char) ? )
608 (> (point) start))
609 (forward-char -1))
610 (when (> (point) start)
611 (forward-char 1))))
612
613 (defun eww-beginning-of-field ()
614 (cond
615 ((bobp)
616 (point))
617 ((not (eq (get-text-property (point) 'eww-form)
618 (get-text-property (1- (point)) 'eww-form)))
619 (point))
620 (t
621 (previous-single-property-change
622 (point) 'eww-form nil (point-min)))))
623
624 (defun eww-end-of-field ()
625 (1- (next-single-property-change
626 (point) 'eww-form nil (point-max))))
627
628 (defun eww-tag-form (cont)
629 (let ((eww-form
630 (list (assq :method cont)
631 (assq :action cont)))
632 (start (point)))
633 (shr-ensure-paragraph)
634 (shr-generic cont)
635 (unless (bolp)
636 (insert "\n"))
637 (insert "\n")
638 (when (> (point) start)
639 (put-text-property start (1+ start)
640 'eww-form eww-form))))
641
642 (defun eww-form-submit (cont)
643 (let ((start (point))
644 (value (cdr (assq :value cont))))
645 (setq value
646 (if (zerop (length value))
647 "Submit"
648 value))
649 (insert value)
650 (add-face-text-property start (point) 'eww-form-submit)
651 (put-text-property start (point) 'eww-form
652 (list :eww-form eww-form
653 :value value
654 :type "submit"
655 :name (cdr (assq :name cont))))
656 (put-text-property start (point) 'keymap eww-submit-map)
657 (insert " ")))
658
659 (defun eww-form-checkbox (cont)
660 (let ((start (point)))
661 (if (cdr (assq :checked cont))
662 (insert eww-form-checkbox-selected-symbol)
663 (insert eww-form-checkbox-symbol))
664 (add-face-text-property start (point) 'eww-form-checkbox)
665 (put-text-property start (point) 'eww-form
666 (list :eww-form eww-form
667 :value (cdr (assq :value cont))
668 :type (downcase (cdr (assq :type cont)))
669 :checked (cdr (assq :checked cont))
670 :name (cdr (assq :name cont))))
671 (put-text-property start (point) 'keymap eww-checkbox-map)
672 (insert " ")))
673
674 (defun eww-form-text (cont)
675 (let ((start (point))
676 (type (downcase (or (cdr (assq :type cont))
677 "text")))
678 (value (or (cdr (assq :value cont)) ""))
679 (width (string-to-number
680 (or (cdr (assq :size cont))
681 "40"))))
682 (insert value)
683 (when (< (length value) width)
684 (insert (make-string (- width (length value)) ? )))
685 (put-text-property start (point) 'face 'eww-form-text)
686 (put-text-property start (point) 'local-map eww-text-map)
687 (put-text-property start (point) 'inhibit-read-only t)
688 (put-text-property start (point) 'eww-form
689 (list :eww-form eww-form
690 :value value
691 :type type
692 :name (cdr (assq :name cont))))
693 (insert " ")))
694
695 (defconst eww-text-input-types '("text" "password" "textarea"
696 "color" "date" "datetime" "datetime-local"
697 "email" "month" "number" "search" "tel"
698 "time" "url" "week")
699 "List of input types which represent a text input.
700 See URL `https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Input'.")
701
702 (defun eww-process-text-input (beg end length)
703 (let* ((form (get-text-property (min (1+ end) (point-max)) 'eww-form))
704 (properties (text-properties-at end))
705 (type (plist-get form :type)))
706 (when (and form
707 (member type eww-text-input-types))
708 (cond
709 ((zerop length)
710 ;; Delete some space at the end.
711 (save-excursion
712 (goto-char
713 (if (equal type "textarea")
714 (1- (line-end-position))
715 (eww-end-of-field)))
716 (let ((new (- end beg)))
717 (while (and (> new 0)
718 (eql (following-char) ? ))
719 (delete-region (point) (1+ (point)))
720 (setq new (1- new))))
721 (set-text-properties beg end properties)))
722 ((> length 0)
723 ;; Add padding.
724 (save-excursion
725 (goto-char
726 (if (equal type "textarea")
727 (1- (line-end-position))
728 (eww-end-of-field)))
729 (let ((start (point)))
730 (insert (make-string length ? ))
731 (set-text-properties start (point) properties)))))
732 (let ((value (buffer-substring-no-properties
733 (eww-beginning-of-field)
734 (eww-end-of-field))))
735 (when (string-match " +\\'" value)
736 (setq value (substring value 0 (match-beginning 0))))
737 (plist-put form :value value)
738 (when (equal type "password")
739 ;; Display passwords as asterisks.
740 (let ((start (eww-beginning-of-field)))
741 (put-text-property start (+ start (length value))
742 'display (make-string (length value) ?*))))))))
743
744 (defun eww-tag-textarea (cont)
745 (let ((start (point))
746 (value (or (cdr (assq :value cont)) ""))
747 (lines (string-to-number
748 (or (cdr (assq :rows cont))
749 "10")))
750 (width (string-to-number
751 (or (cdr (assq :cols cont))
752 "10")))
753 end)
754 (shr-ensure-newline)
755 (insert value)
756 (shr-ensure-newline)
757 (when (< (count-lines start (point)) lines)
758 (dotimes (i (- lines (count-lines start (point))))
759 (insert "\n")))
760 (setq end (point-marker))
761 (goto-char start)
762 (while (< (point) end)
763 (end-of-line)
764 (let ((pad (- width (- (point) (line-beginning-position)))))
765 (when (> pad 0)
766 (insert (make-string pad ? ))))
767 (add-face-text-property (line-beginning-position)
768 (point) 'eww-form-text)
769 (put-text-property (line-beginning-position) (point)
770 'local-map eww-textarea-map)
771 (forward-line 1))
772 (put-text-property start (point) 'eww-form
773 (list :eww-form eww-form
774 :value value
775 :type "textarea"
776 :name (cdr (assq :name cont))))))
777
778 (defun eww-tag-input (cont)
779 (let ((type (downcase (or (cdr (assq :type cont))
780 "text")))
781 (start (point)))
782 (cond
783 ((or (equal type "checkbox")
784 (equal type "radio"))
785 (eww-form-checkbox cont))
786 ((equal type "submit")
787 (eww-form-submit cont))
788 ((equal type "hidden")
789 (let ((form eww-form)
790 (name (cdr (assq :name cont))))
791 ;; Don't add <input type=hidden> elements repeatedly.
792 (while (and form
793 (or (not (consp (car form)))
794 (not (eq (caar form) 'hidden))
795 (not (equal (plist-get (cdr (car form)) :name)
796 name))))
797 (setq form (cdr form)))
798 (unless form
799 (nconc eww-form (list
800 (list 'hidden
801 :name name
802 :value (cdr (assq :value cont))))))))
803 (t
804 (eww-form-text cont)))
805 (unless (= start (point))
806 (put-text-property start (1+ start) 'help-echo "Input field"))))
807
808 (defun eww-tag-select (cont)
809 (shr-ensure-paragraph)
810 (let ((menu (list :name (cdr (assq :name cont))
811 :eww-form eww-form))
812 (options nil)
813 (start (point))
814 (max 0)
815 opelem)
816 (if (eq (car (car cont)) 'optgroup)
817 (dolist (groupelem cont)
818 (unless (cdr (assq :disabled (cdr groupelem)))
819 (setq opelem (append opelem (cdr (cdr groupelem))))))
820 (setq opelem cont))
821 (dolist (elem opelem)
822 (when (eq (car elem) 'option)
823 (when (cdr (assq :selected (cdr elem)))
824 (nconc menu (list :value
825 (cdr (assq :value (cdr elem))))))
826 (let ((display (or (cdr (assq 'text (cdr elem))) "")))
827 (setq max (max max (length display)))
828 (push (list 'item
829 :value (cdr (assq :value (cdr elem)))
830 :display display)
831 options))))
832 (when options
833 (setq options (nreverse options))
834 ;; If we have no selected values, default to the first value.
835 (unless (plist-get menu :value)
836 (nconc menu (list :value (nth 2 (car options)))))
837 (nconc menu options)
838 (let ((selected (eww-select-display menu)))
839 (insert selected
840 (make-string (- max (length selected)) ? )))
841 (put-text-property start (point) 'eww-form menu)
842 (add-face-text-property start (point) 'eww-form-select)
843 (put-text-property start (point) 'keymap eww-select-map)
844 (shr-ensure-paragraph))))
845
846 (defun eww-select-display (select)
847 (let ((value (plist-get select :value))
848 display)
849 (dolist (elem select)
850 (when (and (consp elem)
851 (eq (car elem) 'item)
852 (equal value (plist-get (cdr elem) :value)))
853 (setq display (plist-get (cdr elem) :display))))
854 display))
855
856 (defun eww-change-select ()
857 "Change the value of the select drop-down menu under point."
858 (interactive)
859 (let* ((input (get-text-property (point) 'eww-form))
860 (completion-ignore-case t)
861 (options
862 (delq nil
863 (mapcar (lambda (elem)
864 (and (consp elem)
865 (eq (car elem) 'item)
866 (cons (plist-get (cdr elem) :display)
867 (plist-get (cdr elem) :value))))
868 input)))
869 (display
870 (completing-read "Change value: " options nil 'require-match))
871 (inhibit-read-only t))
872 (plist-put input :value (cdr (assoc-string display options t)))
873 (goto-char
874 (eww-update-field display))))
875
876 (defun eww-update-field (string)
877 (let ((properties (text-properties-at (point)))
878 (start (eww-beginning-of-field))
879 (end (1+ (eww-end-of-field))))
880 (delete-region start end)
881 (insert string
882 (make-string (- (- end start) (length string)) ? ))
883 (set-text-properties start end properties)
884 start))
885
886 (defun eww-toggle-checkbox ()
887 "Toggle the value of the checkbox under point."
888 (interactive)
889 (let* ((input (get-text-property (point) 'eww-form))
890 (type (plist-get input :type)))
891 (if (equal type "checkbox")
892 (goto-char
893 (1+
894 (if (plist-get input :checked)
895 (progn
896 (plist-put input :checked nil)
897 (eww-update-field eww-form-checkbox-symbol))
898 (plist-put input :checked t)
899 (eww-update-field eww-form-checkbox-selected-symbol))))
900 ;; Radio button. Switch all other buttons off.
901 (let ((name (plist-get input :name)))
902 (save-excursion
903 (dolist (elem (eww-inputs (plist-get input :eww-form)))
904 (when (equal (plist-get (cdr elem) :name) name)
905 (goto-char (car elem))
906 (if (not (eq (cdr elem) input))
907 (progn
908 (plist-put input :checked nil)
909 (eww-update-field eww-form-checkbox-symbol))
910 (plist-put input :checked t)
911 (eww-update-field eww-form-checkbox-selected-symbol)))))
912 (forward-char 1)))))
913
914 (defun eww-inputs (form)
915 (let ((start (point-min))
916 (inputs nil))
917 (while (and start
918 (< start (point-max)))
919 (when (or (get-text-property start 'eww-form)
920 (setq start (next-single-property-change start 'eww-form)))
921 (when (eq (plist-get (get-text-property start 'eww-form) :eww-form)
922 form)
923 (push (cons start (get-text-property start 'eww-form))
924 inputs))
925 (setq start (next-single-property-change start 'eww-form))))
926 (nreverse inputs)))
927
928 (defun eww-input-value (input)
929 (let ((type (plist-get input :type))
930 (value (plist-get input :value)))
931 (cond
932 ((equal type "textarea")
933 (with-temp-buffer
934 (insert value)
935 (goto-char (point-min))
936 (while (re-search-forward "^ +\n\\| +$" nil t)
937 (replace-match "" t t))
938 (buffer-string)))
939 (t
940 (if (string-match " +\\'" value)
941 (substring value 0 (match-beginning 0))
942 value)))))
943
944 (defun eww-submit ()
945 "Submit the current form."
946 (interactive)
947 (let* ((this-input (get-text-property (point) 'eww-form))
948 (form (plist-get this-input :eww-form))
949 values next-submit)
950 (dolist (elem (sort (eww-inputs form)
951 (lambda (o1 o2)
952 (< (car o1) (car o2)))))
953 (let* ((input (cdr elem))
954 (input-start (car elem))
955 (name (plist-get input :name)))
956 (when name
957 (cond
958 ((member (plist-get input :type) '("checkbox" "radio"))
959 (when (plist-get input :checked)
960 (push (cons name (plist-get input :value))
961 values)))
962 ((equal (plist-get input :type) "submit")
963 ;; We want the values from buttons if we hit a button if
964 ;; we hit enter on it, or if it's the first button after
965 ;; the field we did hit return on.
966 (when (or (eq input this-input)
967 (and (not (eq input this-input))
968 (null next-submit)
969 (> input-start (point))))
970 (setq next-submit t)
971 (push (cons name (plist-get input :value))
972 values)))
973 (t
974 (push (cons name (eww-input-value input))
975 values))))))
976 (dolist (elem form)
977 (when (and (consp elem)
978 (eq (car elem) 'hidden))
979 (push (cons (plist-get (cdr elem) :name)
980 (plist-get (cdr elem) :value))
981 values)))
982 (if (and (stringp (cdr (assq :method form)))
983 (equal (downcase (cdr (assq :method form))) "post"))
984 (let ((url-request-method "POST")
985 (url-request-extra-headers
986 '(("Content-Type" . "application/x-www-form-urlencoded")))
987 (url-request-data (mm-url-encode-www-form-urlencoded values)))
988 (eww-browse-url (shr-expand-url (cdr (assq :action form))
989 eww-current-url)))
990 (eww-browse-url
991 (concat
992 (if (cdr (assq :action form))
993 (shr-expand-url (cdr (assq :action form))
994 eww-current-url)
995 eww-current-url)
996 "?"
997 (mm-url-encode-www-form-urlencoded values))))))
998
999 (defun eww-browse-with-external-browser (&optional url)
1000 "Browse the current URL with an external browser.
1001 The browser to used is specified by the `shr-external-browser' variable."
1002 (interactive)
1003 (funcall shr-external-browser (or url eww-current-url)))
1004
1005 (defun eww-follow-link (&optional external mouse-event)
1006 "Browse the URL under point.
1007 If EXTERNAL, browse the URL using `shr-external-browser'."
1008 (interactive (list current-prefix-arg last-nonmenu-event))
1009 (mouse-set-point mouse-event)
1010 (let ((url (get-text-property (point) 'shr-url)))
1011 (cond
1012 ((not url)
1013 (message "No link under point"))
1014 ((string-match "^mailto:" url)
1015 (browse-url-mail url))
1016 (external
1017 (funcall shr-external-browser url))
1018 ;; This is a #target url in the same page as the current one.
1019 ((and (url-target (url-generic-parse-url url))
1020 (eww-same-page-p url eww-current-url))
1021 (eww-save-history)
1022 (eww-display-html 'utf8 url eww-current-dom))
1023 (t
1024 (eww-browse-url url)))))
1025
1026 (defun eww-same-page-p (url1 url2)
1027 "Return non-nil if both URLs represent the same page.
1028 Differences in #targets are ignored."
1029 (let ((obj1 (url-generic-parse-url url1))
1030 (obj2 (url-generic-parse-url url2)))
1031 (setf (url-target obj1) nil)
1032 (setf (url-target obj2) nil)
1033 (equal (url-recreate-url obj1) (url-recreate-url obj2))))
1034
1035 (defun eww-copy-page-url ()
1036 (interactive)
1037 (message "%s" eww-current-url)
1038 (kill-new eww-current-url))
1039
1040 (defun eww-download ()
1041 "Download URL under point to `eww-download-directory'."
1042 (interactive)
1043 (let ((url (get-text-property (point) 'shr-url)))
1044 (if (not url)
1045 (message "No URL under point")
1046 (url-retrieve url 'eww-download-callback (list url)))))
1047
1048 (defun eww-download-callback (status url)
1049 (unless (plist-get status :error)
1050 (let* ((obj (url-generic-parse-url url))
1051 (path (car (url-path-and-query obj)))
1052 (file (eww-make-unique-file-name (file-name-nondirectory path)
1053 eww-download-path)))
1054 (write-file file)
1055 (message "Saved %s" file))))
1056
1057 (defun eww-make-unique-file-name (file directory)
1058 (cond
1059 ((zerop (length file))
1060 (setq file "!"))
1061 ((string-match "\\`[.]" file)
1062 (setq file (concat "!" file))))
1063 (let ((count 1))
1064 (while (file-exists-p (expand-file-name file directory))
1065 (setq file
1066 (if (string-match "\\`\\(.*\\)\\([.][^.]+\\)" file)
1067 (format "%s(%d)%s" (match-string 1 file)
1068 count (match-string 2 file))
1069 (format "%s(%d)" file count)))
1070 (setq count (1+ count)))
1071 (expand-file-name file directory)))
1072
1073 ;;; Bookmarks code
1074
1075 (defvar eww-bookmarks nil)
1076
1077 (defun eww-add-bookmark ()
1078 "Add the current page to the bookmarks."
1079 (interactive)
1080 (eww-read-bookmarks)
1081 (dolist (bookmark eww-bookmarks)
1082 (when (equal eww-current-url
1083 (plist-get bookmark :url))
1084 (error "Already bookmarked")))
1085 (if (y-or-n-p "bookmark this page? ")
1086 (progn
1087 (let ((title (replace-regexp-in-string "[\n\t\r]" " " eww-current-title)))
1088 (setq title (replace-regexp-in-string "\\` +\\| +\\'" "" title))
1089 (push (list :url eww-current-url
1090 :title title
1091 :time (current-time-string))
1092 eww-bookmarks))
1093 (eww-write-bookmarks)
1094 (message "Bookmarked %s (%s)" eww-current-url eww-current-title))))
1095
1096 (defun eww-write-bookmarks ()
1097 (with-temp-file (expand-file-name "eww-bookmarks" user-emacs-directory)
1098 (insert ";; Auto-generated file; don't edit\n")
1099 (pp eww-bookmarks (current-buffer))))
1100
1101 (defun eww-read-bookmarks ()
1102 (let ((file (expand-file-name "eww-bookmarks" user-emacs-directory)))
1103 (setq eww-bookmarks
1104 (unless (zerop (or (nth 7 (file-attributes file)) 0))
1105 (with-temp-buffer
1106 (insert-file-contents file)
1107 (read (current-buffer)))))))
1108
1109 (defun eww-list-bookmarks ()
1110 "Display the bookmarks."
1111 (interactive)
1112 (eww-bookmark-prepare)
1113 (pop-to-buffer "*eww bookmarks*"))
1114
1115 (defun eww-bookmark-prepare ()
1116 (eww-read-bookmarks)
1117 (when (null eww-bookmarks)
1118 (error "No bookmarks are defined"))
1119 (set-buffer (get-buffer-create "*eww bookmarks*"))
1120 (eww-bookmark-mode)
1121 (let ((format "%-40s %s")
1122 (inhibit-read-only t)
1123 start url)
1124 (erase-buffer)
1125 (setq header-line-format (concat " " (format format "URL" "Title")))
1126 (dolist (bookmark eww-bookmarks)
1127 (setq start (point))
1128 (setq url (plist-get bookmark :url))
1129 (when (> (length url) 40)
1130 (setq url (substring url 0 40)))
1131 (insert (format format url
1132 (plist-get bookmark :title))
1133 "\n")
1134 (put-text-property start (1+ start) 'eww-bookmark bookmark))
1135 (goto-char (point-min))))
1136
1137 (defvar eww-bookmark-kill-ring nil)
1138
1139 (defun eww-bookmark-kill ()
1140 "Kill the current bookmark."
1141 (interactive)
1142 (let* ((start (line-beginning-position))
1143 (bookmark (get-text-property start 'eww-bookmark))
1144 (inhibit-read-only t))
1145 (unless bookmark
1146 (error "No bookmark on the current line"))
1147 (forward-line 1)
1148 (push (buffer-substring start (point)) eww-bookmark-kill-ring)
1149 (delete-region start (point))
1150 (setq eww-bookmarks (delq bookmark eww-bookmarks))
1151 (eww-write-bookmarks)))
1152
1153 (defun eww-bookmark-yank ()
1154 "Yank a previously killed bookmark to the current line."
1155 (interactive)
1156 (unless eww-bookmark-kill-ring
1157 (error "No previously killed bookmark"))
1158 (beginning-of-line)
1159 (let ((inhibit-read-only t)
1160 (start (point))
1161 bookmark)
1162 (insert (pop eww-bookmark-kill-ring))
1163 (setq bookmark (get-text-property start 'eww-bookmark))
1164 (if (= start (point-min))
1165 (push bookmark eww-bookmarks)
1166 (let ((line (count-lines start (point))))
1167 (setcdr (nthcdr (1- line) eww-bookmarks)
1168 (cons bookmark (nthcdr line eww-bookmarks)))))
1169 (eww-write-bookmarks)))
1170
1171 (defun eww-bookmark-quit ()
1172 "Kill the current buffer."
1173 (interactive)
1174 (kill-buffer (current-buffer)))
1175
1176 (defun eww-bookmark-browse ()
1177 "Browse the bookmark under point in eww."
1178 (interactive)
1179 (let ((bookmark (get-text-property (line-beginning-position) 'eww-bookmark)))
1180 (unless bookmark
1181 (error "No bookmark on the current line"))
1182 ;; We wish to leave this window, but if it's the only window here,
1183 ;; just let it remain.
1184 (ignore-errors
1185 (delete-window))
1186 (eww-browse-url (plist-get bookmark :url))))
1187
1188 (defun eww-next-bookmark ()
1189 "Go to the next bookmark in the list."
1190 (interactive)
1191 (let ((first nil)
1192 bookmark)
1193 (unless (get-buffer "*eww bookmarks*")
1194 (setq first t)
1195 (eww-bookmark-prepare))
1196 (with-current-buffer (get-buffer "*eww bookmarks*")
1197 (when (and (not first)
1198 (not (eobp)))
1199 (forward-line 1))
1200 (setq bookmark (get-text-property (line-beginning-position)
1201 'eww-bookmark))
1202 (unless bookmark
1203 (error "No next bookmark")))
1204 (eww-browse-url (plist-get bookmark :url))))
1205
1206 (defun eww-previous-bookmark ()
1207 "Go to the previous bookmark in the list."
1208 (interactive)
1209 (let ((first nil)
1210 bookmark)
1211 (unless (get-buffer "*eww bookmarks*")
1212 (setq first t)
1213 (eww-bookmark-prepare))
1214 (with-current-buffer (get-buffer "*eww bookmarks*")
1215 (if first
1216 (goto-char (point-max))
1217 (beginning-of-line))
1218 ;; On the final line.
1219 (when (eolp)
1220 (forward-line -1))
1221 (if (bobp)
1222 (error "No previous bookmark")
1223 (forward-line -1))
1224 (setq bookmark (get-text-property (line-beginning-position)
1225 'eww-bookmark)))
1226 (eww-browse-url (plist-get bookmark :url))))
1227
1228 (defvar eww-bookmark-mode-map
1229 (let ((map (make-sparse-keymap)))
1230 (suppress-keymap map)
1231 (define-key map "q" 'eww-bookmark-quit)
1232 (define-key map [(control k)] 'eww-bookmark-kill)
1233 (define-key map [(control y)] 'eww-bookmark-yank)
1234 (define-key map "\r" 'eww-bookmark-browse)
1235 map))
1236
1237 (define-derived-mode eww-bookmark-mode nil "eww bookmarks"
1238 "Mode for listing bookmarks.
1239
1240 \\{eww-bookmark-mode-map}"
1241 (buffer-disable-undo)
1242 (setq buffer-read-only t
1243 truncate-lines t))
1244
1245 (provide 'eww)
1246
1247 ;;; eww.el ends here