eww easy-menu fixes
[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" 'quit-window)
396 (define-key map "g" 'eww-reload)
397 (define-key map [tab] 'shr-next-link)
398 (define-key map [backtab] 'shr-previous-link)
399 (define-key map [delete] 'scroll-down-command)
400 (define-key map "\177" 'scroll-down-command)
401 (define-key map " " 'scroll-up-command)
402 (define-key map "l" 'eww-back-url)
403 (define-key map "f" 'eww-forward-url)
404 (define-key map "n" 'eww-next-url)
405 (define-key map "p" 'eww-previous-url)
406 (define-key map "u" 'eww-up-url)
407 (define-key map "t" 'eww-top-url)
408 (define-key map "&" 'eww-browse-with-external-browser)
409 (define-key map "d" 'eww-download)
410 (define-key map "w" 'eww-copy-page-url)
411 (define-key map "C" 'url-cookie-list)
412 (define-key map "v" 'eww-view-source)
413
414 (define-key map "b" 'eww-add-bookmark)
415 (define-key map "B" 'eww-list-bookmarks)
416 (define-key map [(meta n)] 'eww-next-bookmark)
417 (define-key map [(meta p)] 'eww-previous-bookmark)
418
419 (easy-menu-define nil map ""
420 '("Eww"
421 ["Exit" eww-quit t]
422 ["Close browser" quit-window t]
423 ["Reload" eww-reload t]
424 ["Back to previous page" eww-back-url
425 :active (not (zerop (length eww-history)))]
426 ["Forward to next page" eww-forward-url
427 :active (not (zerop eww-history-position))]
428 ["Browse with external browser" eww-browse-with-external-browser t]
429 ["Download" eww-download t]
430 ["View page source" eww-view-source]
431 ["Copy page URL" eww-copy-page-url t]
432 ["Add bookmark" eww-add-bookmark t]
433 ["List bookmarks" eww-list-bookmarks t]
434 ["List cookies" url-cookie-list t]))
435 map))
436
437 (define-derived-mode eww-mode nil "eww"
438 "Mode for browsing the web.
439
440 \\{eww-mode-map}"
441 ;; FIXME? This seems a strange default.
442 (set (make-local-variable 'eww-current-url) 'author)
443 (set (make-local-variable 'eww-current-dom) nil)
444 (set (make-local-variable 'eww-current-source) nil)
445 (set (make-local-variable 'browse-url-browser-function) 'eww-browse-url)
446 (set (make-local-variable 'after-change-functions) 'eww-process-text-input)
447 (set (make-local-variable 'eww-history) nil)
448 (set (make-local-variable 'eww-history-position) 0)
449 (buffer-disable-undo)
450 ;;(setq buffer-read-only t)
451 )
452
453 (defun eww-save-history ()
454 (push (list :url eww-current-url
455 :title eww-current-title
456 :point (point)
457 :dom eww-current-dom
458 :source eww-current-source
459 :text (buffer-string))
460 eww-history))
461
462 ;;;###autoload
463 (defun eww-browse-url (url &optional _new-window)
464 (when (and (equal major-mode 'eww-mode)
465 eww-current-url)
466 (eww-save-history))
467 (eww url))
468
469 (defun eww-back-url ()
470 "Go to the previously displayed page."
471 (interactive)
472 (when (>= eww-history-position (length eww-history))
473 (error "No previous page"))
474 (eww-save-history)
475 (setq eww-history-position (+ eww-history-position 2))
476 (eww-restore-history (elt eww-history (1- eww-history-position))))
477
478 (defun eww-forward-url ()
479 "Go to the next displayed page."
480 (interactive)
481 (when (zerop eww-history-position)
482 (error "No next page"))
483 (eww-save-history)
484 (eww-restore-history (elt eww-history (1- eww-history-position))))
485
486 (defun eww-restore-history (elem)
487 (let ((inhibit-read-only t))
488 (erase-buffer)
489 (insert (plist-get elem :text))
490 (setq eww-current-source (plist-get elem :source))
491 (setq eww-current-dom (plist-get elem :dom))
492 (goto-char (plist-get elem :point))
493 (setq eww-current-url (plist-get elem :url)
494 eww-current-title (plist-get elem :title))
495 (eww-update-header-line-format)))
496
497 (defun eww-next-url ()
498 "Go to the page marked `next'.
499 A page is marked `next' if rel=\"next\" appears in a <link>
500 or <a> tag."
501 (interactive)
502 (if eww-next-url
503 (eww-browse-url (shr-expand-url eww-next-url eww-current-url))
504 (error "No `next' on this page")))
505
506 (defun eww-previous-url ()
507 "Go to the page marked `previous'.
508 A page is marked `previous' if rel=\"previous\" appears in a <link>
509 or <a> tag."
510 (interactive)
511 (if eww-previous-url
512 (eww-browse-url (shr-expand-url eww-previous-url eww-current-url))
513 (error "No `previous' on this page")))
514
515 (defun eww-up-url ()
516 "Go to the page marked `up'.
517 A page is marked `up' if rel=\"up\" appears in a <link>
518 or <a> tag."
519 (interactive)
520 (if eww-up-url
521 (eww-browse-url (shr-expand-url eww-up-url eww-current-url))
522 (error "No `up' on this page")))
523
524 (defun eww-top-url ()
525 "Go to the page marked `top'.
526 A page is marked `top' if rel=\"start\", rel=\"home\", or rel=\"contents\"
527 appears in a <link> or <a> tag."
528 (interactive)
529 (let ((best-url (or eww-start-url
530 eww-contents-url
531 eww-home-url)))
532 (if best-url
533 (eww-browse-url (shr-expand-url best-url eww-current-url))
534 (error "No `top' for this page"))))
535
536 (defun eww-reload ()
537 "Reload the current page."
538 (interactive)
539 (url-retrieve eww-current-url 'eww-render
540 (list eww-current-url (point))))
541
542 ;; Form support.
543
544 (defvar eww-form nil)
545
546 (defvar eww-submit-map
547 (let ((map (make-sparse-keymap)))
548 (define-key map "\r" 'eww-submit)
549 (define-key map [(control c) (control c)] 'eww-submit)
550 map))
551
552 (defvar eww-checkbox-map
553 (let ((map (make-sparse-keymap)))
554 (define-key map [space] 'eww-toggle-checkbox)
555 (define-key map "\r" 'eww-toggle-checkbox)
556 (define-key map [(control c) (control c)] 'eww-submit)
557 map))
558
559 (defvar eww-text-map
560 (let ((map (make-keymap)))
561 (set-keymap-parent map text-mode-map)
562 (define-key map "\r" 'eww-submit)
563 (define-key map [(control a)] 'eww-beginning-of-text)
564 (define-key map [(control c) (control c)] 'eww-submit)
565 (define-key map [(control e)] 'eww-end-of-text)
566 (define-key map [tab] 'shr-next-link)
567 (define-key map [backtab] 'shr-previous-link)
568 map))
569
570 (defvar eww-textarea-map
571 (let ((map (make-keymap)))
572 (set-keymap-parent map text-mode-map)
573 (define-key map "\r" 'forward-line)
574 (define-key map [(control c) (control c)] 'eww-submit)
575 (define-key map [tab] 'shr-next-link)
576 (define-key map [backtab] 'shr-previous-link)
577 map))
578
579 (defvar eww-select-map
580 (let ((map (make-sparse-keymap)))
581 (define-key map "\r" 'eww-change-select)
582 (define-key map [(control c) (control c)] 'eww-submit)
583 map))
584
585 (defun eww-beginning-of-text ()
586 "Move to the start of the input field."
587 (interactive)
588 (goto-char (eww-beginning-of-field)))
589
590 (defun eww-end-of-text ()
591 "Move to the end of the text in the input field."
592 (interactive)
593 (goto-char (eww-end-of-field))
594 (let ((start (eww-beginning-of-field)))
595 (while (and (equal (following-char) ? )
596 (> (point) start))
597 (forward-char -1))
598 (when (> (point) start)
599 (forward-char 1))))
600
601 (defun eww-beginning-of-field ()
602 (cond
603 ((bobp)
604 (point))
605 ((not (eq (get-text-property (point) 'eww-form)
606 (get-text-property (1- (point)) 'eww-form)))
607 (point))
608 (t
609 (previous-single-property-change
610 (point) 'eww-form nil (point-min)))))
611
612 (defun eww-end-of-field ()
613 (1- (next-single-property-change
614 (point) 'eww-form nil (point-max))))
615
616 (defun eww-tag-form (cont)
617 (let ((eww-form
618 (list (assq :method cont)
619 (assq :action cont)))
620 (start (point)))
621 (shr-ensure-paragraph)
622 (shr-generic cont)
623 (unless (bolp)
624 (insert "\n"))
625 (insert "\n")
626 (when (> (point) start)
627 (put-text-property start (1+ start)
628 'eww-form eww-form))))
629
630 (defun eww-form-submit (cont)
631 (let ((start (point))
632 (value (cdr (assq :value cont))))
633 (setq value
634 (if (zerop (length value))
635 "Submit"
636 value))
637 (insert value)
638 (add-face-text-property start (point) 'eww-form-submit)
639 (put-text-property start (point) 'eww-form
640 (list :eww-form eww-form
641 :value value
642 :type "submit"
643 :name (cdr (assq :name cont))))
644 (put-text-property start (point) 'keymap eww-submit-map)
645 (insert " ")))
646
647 (defun eww-form-checkbox (cont)
648 (let ((start (point)))
649 (if (cdr (assq :checked cont))
650 (insert eww-form-checkbox-selected-symbol)
651 (insert eww-form-checkbox-symbol))
652 (add-face-text-property start (point) 'eww-form-checkbox)
653 (put-text-property start (point) 'eww-form
654 (list :eww-form eww-form
655 :value (cdr (assq :value cont))
656 :type (downcase (cdr (assq :type cont)))
657 :checked (cdr (assq :checked cont))
658 :name (cdr (assq :name cont))))
659 (put-text-property start (point) 'keymap eww-checkbox-map)
660 (insert " ")))
661
662 (defun eww-form-text (cont)
663 (let ((start (point))
664 (type (downcase (or (cdr (assq :type cont))
665 "text")))
666 (value (or (cdr (assq :value cont)) ""))
667 (width (string-to-number
668 (or (cdr (assq :size cont))
669 "40"))))
670 (insert value)
671 (when (< (length value) width)
672 (insert (make-string (- width (length value)) ? )))
673 (put-text-property start (point) 'face 'eww-form-text)
674 (put-text-property start (point) 'local-map eww-text-map)
675 (put-text-property start (point) 'inhibit-read-only t)
676 (put-text-property start (point) 'eww-form
677 (list :eww-form eww-form
678 :value value
679 :type type
680 :name (cdr (assq :name cont))))
681 (insert " ")))
682
683 (defconst eww-text-input-types '("text" "password" "textarea"
684 "color" "date" "datetime" "datetime-local"
685 "email" "month" "number" "search" "tel"
686 "time" "url" "week")
687 "List of input types which represent a text input.
688 See URL `https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Input'.")
689
690 (defun eww-process-text-input (beg end length)
691 (let* ((form (get-text-property (min (1+ end) (point-max)) 'eww-form))
692 (properties (text-properties-at end))
693 (type (plist-get form :type)))
694 (when (and form
695 (member type eww-text-input-types))
696 (cond
697 ((zerop length)
698 ;; Delete some space at the end.
699 (save-excursion
700 (goto-char
701 (if (equal type "textarea")
702 (1- (line-end-position))
703 (eww-end-of-field)))
704 (let ((new (- end beg)))
705 (while (and (> new 0)
706 (eql (following-char) ? ))
707 (delete-region (point) (1+ (point)))
708 (setq new (1- new))))
709 (set-text-properties beg end properties)))
710 ((> length 0)
711 ;; Add padding.
712 (save-excursion
713 (goto-char
714 (if (equal type "textarea")
715 (1- (line-end-position))
716 (eww-end-of-field)))
717 (let ((start (point)))
718 (insert (make-string length ? ))
719 (set-text-properties start (point) properties)))))
720 (let ((value (buffer-substring-no-properties
721 (eww-beginning-of-field)
722 (eww-end-of-field))))
723 (when (string-match " +\\'" value)
724 (setq value (substring value 0 (match-beginning 0))))
725 (plist-put form :value value)
726 (when (equal type "password")
727 ;; Display passwords as asterisks.
728 (let ((start (eww-beginning-of-field)))
729 (put-text-property start (+ start (length value))
730 'display (make-string (length value) ?*))))))))
731
732 (defun eww-tag-textarea (cont)
733 (let ((start (point))
734 (value (or (cdr (assq :value cont)) ""))
735 (lines (string-to-number
736 (or (cdr (assq :rows cont))
737 "10")))
738 (width (string-to-number
739 (or (cdr (assq :cols cont))
740 "10")))
741 end)
742 (shr-ensure-newline)
743 (insert value)
744 (shr-ensure-newline)
745 (when (< (count-lines start (point)) lines)
746 (dotimes (i (- lines (count-lines start (point))))
747 (insert "\n")))
748 (setq end (point-marker))
749 (goto-char start)
750 (while (< (point) end)
751 (end-of-line)
752 (let ((pad (- width (- (point) (line-beginning-position)))))
753 (when (> pad 0)
754 (insert (make-string pad ? ))))
755 (add-face-text-property (line-beginning-position)
756 (point) 'eww-form-text)
757 (put-text-property (line-beginning-position) (point)
758 'local-map eww-textarea-map)
759 (forward-line 1))
760 (put-text-property start (point) 'eww-form
761 (list :eww-form eww-form
762 :value value
763 :type "textarea"
764 :name (cdr (assq :name cont))))))
765
766 (defun eww-tag-input (cont)
767 (let ((type (downcase (or (cdr (assq :type cont))
768 "text")))
769 (start (point)))
770 (cond
771 ((or (equal type "checkbox")
772 (equal type "radio"))
773 (eww-form-checkbox cont))
774 ((equal type "submit")
775 (eww-form-submit cont))
776 ((equal type "hidden")
777 (let ((form eww-form)
778 (name (cdr (assq :name cont))))
779 ;; Don't add <input type=hidden> elements repeatedly.
780 (while (and form
781 (or (not (consp (car form)))
782 (not (eq (caar form) 'hidden))
783 (not (equal (plist-get (cdr (car form)) :name)
784 name))))
785 (setq form (cdr form)))
786 (unless form
787 (nconc eww-form (list
788 (list 'hidden
789 :name name
790 :value (cdr (assq :value cont))))))))
791 (t
792 (eww-form-text cont)))
793 (unless (= start (point))
794 (put-text-property start (1+ start) 'help-echo "Input field"))))
795
796 (defun eww-tag-select (cont)
797 (shr-ensure-paragraph)
798 (let ((menu (list :name (cdr (assq :name cont))
799 :eww-form eww-form))
800 (options nil)
801 (start (point))
802 (max 0)
803 opelem)
804 (if (eq (car (car cont)) 'optgroup)
805 (dolist (groupelem cont)
806 (unless (cdr (assq :disabled (cdr groupelem)))
807 (setq opelem (append opelem (cdr (cdr groupelem))))))
808 (setq opelem cont))
809 (dolist (elem opelem)
810 (when (eq (car elem) 'option)
811 (when (cdr (assq :selected (cdr elem)))
812 (nconc menu (list :value
813 (cdr (assq :value (cdr elem))))))
814 (let ((display (or (cdr (assq 'text (cdr elem))) "")))
815 (setq max (max max (length display)))
816 (push (list 'item
817 :value (cdr (assq :value (cdr elem)))
818 :display display)
819 options))))
820 (when options
821 (setq options (nreverse options))
822 ;; If we have no selected values, default to the first value.
823 (unless (plist-get menu :value)
824 (nconc menu (list :value (nth 2 (car options)))))
825 (nconc menu options)
826 (let ((selected (eww-select-display menu)))
827 (insert selected
828 (make-string (- max (length selected)) ? )))
829 (put-text-property start (point) 'eww-form menu)
830 (add-face-text-property start (point) 'eww-form-select)
831 (put-text-property start (point) 'keymap eww-select-map)
832 (shr-ensure-paragraph))))
833
834 (defun eww-select-display (select)
835 (let ((value (plist-get select :value))
836 display)
837 (dolist (elem select)
838 (when (and (consp elem)
839 (eq (car elem) 'item)
840 (equal value (plist-get (cdr elem) :value)))
841 (setq display (plist-get (cdr elem) :display))))
842 display))
843
844 (defun eww-change-select ()
845 "Change the value of the select drop-down menu under point."
846 (interactive)
847 (let* ((input (get-text-property (point) 'eww-form))
848 (completion-ignore-case t)
849 (options
850 (delq nil
851 (mapcar (lambda (elem)
852 (and (consp elem)
853 (eq (car elem) 'item)
854 (cons (plist-get (cdr elem) :display)
855 (plist-get (cdr elem) :value))))
856 input)))
857 (display
858 (completing-read "Change value: " options nil 'require-match))
859 (inhibit-read-only t))
860 (plist-put input :value (cdr (assoc-string display options t)))
861 (goto-char
862 (eww-update-field display))))
863
864 (defun eww-update-field (string)
865 (let ((properties (text-properties-at (point)))
866 (start (eww-beginning-of-field))
867 (end (1+ (eww-end-of-field))))
868 (delete-region start end)
869 (insert string
870 (make-string (- (- end start) (length string)) ? ))
871 (set-text-properties start end properties)
872 start))
873
874 (defun eww-toggle-checkbox ()
875 "Toggle the value of the checkbox under point."
876 (interactive)
877 (let* ((input (get-text-property (point) 'eww-form))
878 (type (plist-get input :type)))
879 (if (equal type "checkbox")
880 (goto-char
881 (1+
882 (if (plist-get input :checked)
883 (progn
884 (plist-put input :checked nil)
885 (eww-update-field eww-form-checkbox-symbol))
886 (plist-put input :checked t)
887 (eww-update-field eww-form-checkbox-selected-symbol))))
888 ;; Radio button. Switch all other buttons off.
889 (let ((name (plist-get input :name)))
890 (save-excursion
891 (dolist (elem (eww-inputs (plist-get input :eww-form)))
892 (when (equal (plist-get (cdr elem) :name) name)
893 (goto-char (car elem))
894 (if (not (eq (cdr elem) input))
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 (forward-char 1)))))
901
902 (defun eww-inputs (form)
903 (let ((start (point-min))
904 (inputs nil))
905 (while (and start
906 (< start (point-max)))
907 (when (or (get-text-property start 'eww-form)
908 (setq start (next-single-property-change start 'eww-form)))
909 (when (eq (plist-get (get-text-property start 'eww-form) :eww-form)
910 form)
911 (push (cons start (get-text-property start 'eww-form))
912 inputs))
913 (setq start (next-single-property-change start 'eww-form))))
914 (nreverse inputs)))
915
916 (defun eww-input-value (input)
917 (let ((type (plist-get input :type))
918 (value (plist-get input :value)))
919 (cond
920 ((equal type "textarea")
921 (with-temp-buffer
922 (insert value)
923 (goto-char (point-min))
924 (while (re-search-forward "^ +\n\\| +$" nil t)
925 (replace-match "" t t))
926 (buffer-string)))
927 (t
928 (if (string-match " +\\'" value)
929 (substring value 0 (match-beginning 0))
930 value)))))
931
932 (defun eww-submit ()
933 "Submit the current form."
934 (interactive)
935 (let* ((this-input (get-text-property (point) 'eww-form))
936 (form (plist-get this-input :eww-form))
937 values next-submit)
938 (dolist (elem (sort (eww-inputs form)
939 (lambda (o1 o2)
940 (< (car o1) (car o2)))))
941 (let* ((input (cdr elem))
942 (input-start (car elem))
943 (name (plist-get input :name)))
944 (when name
945 (cond
946 ((member (plist-get input :type) '("checkbox" "radio"))
947 (when (plist-get input :checked)
948 (push (cons name (plist-get input :value))
949 values)))
950 ((equal (plist-get input :type) "submit")
951 ;; We want the values from buttons if we hit a button if
952 ;; we hit enter on it, or if it's the first button after
953 ;; the field we did hit return on.
954 (when (or (eq input this-input)
955 (and (not (eq input this-input))
956 (null next-submit)
957 (> input-start (point))))
958 (setq next-submit t)
959 (push (cons name (plist-get input :value))
960 values)))
961 (t
962 (push (cons name (eww-input-value input))
963 values))))))
964 (dolist (elem form)
965 (when (and (consp elem)
966 (eq (car elem) 'hidden))
967 (push (cons (plist-get (cdr elem) :name)
968 (plist-get (cdr elem) :value))
969 values)))
970 (if (and (stringp (cdr (assq :method form)))
971 (equal (downcase (cdr (assq :method form))) "post"))
972 (let ((url-request-method "POST")
973 (url-request-extra-headers
974 '(("Content-Type" . "application/x-www-form-urlencoded")))
975 (url-request-data (mm-url-encode-www-form-urlencoded values)))
976 (eww-browse-url (shr-expand-url (cdr (assq :action form))
977 eww-current-url)))
978 (eww-browse-url
979 (concat
980 (if (cdr (assq :action form))
981 (shr-expand-url (cdr (assq :action form))
982 eww-current-url)
983 eww-current-url)
984 "?"
985 (mm-url-encode-www-form-urlencoded values))))))
986
987 (defun eww-browse-with-external-browser (&optional url)
988 "Browse the current URL with an external browser.
989 The browser to used is specified by the `shr-external-browser' variable."
990 (interactive)
991 (funcall shr-external-browser (or url eww-current-url)))
992
993 (defun eww-follow-link (&optional external mouse-event)
994 "Browse the URL under point.
995 If EXTERNAL, browse the URL using `shr-external-browser'."
996 (interactive (list current-prefix-arg last-nonmenu-event))
997 (mouse-set-point mouse-event)
998 (let ((url (get-text-property (point) 'shr-url)))
999 (cond
1000 ((not url)
1001 (message "No link under point"))
1002 ((string-match "^mailto:" url)
1003 (browse-url-mail url))
1004 (external
1005 (funcall shr-external-browser url))
1006 ;; This is a #target url in the same page as the current one.
1007 ((and (url-target (url-generic-parse-url url))
1008 (eww-same-page-p url eww-current-url))
1009 (eww-save-history)
1010 (eww-display-html 'utf8 url eww-current-dom))
1011 (t
1012 (eww-browse-url url)))))
1013
1014 (defun eww-same-page-p (url1 url2)
1015 "Return non-nil if both URLs represent the same page.
1016 Differences in #targets are ignored."
1017 (let ((obj1 (url-generic-parse-url url1))
1018 (obj2 (url-generic-parse-url url2)))
1019 (setf (url-target obj1) nil)
1020 (setf (url-target obj2) nil)
1021 (equal (url-recreate-url obj1) (url-recreate-url obj2))))
1022
1023 (defun eww-copy-page-url ()
1024 (interactive)
1025 (message "%s" eww-current-url)
1026 (kill-new eww-current-url))
1027
1028 (defun eww-download ()
1029 "Download URL under point to `eww-download-directory'."
1030 (interactive)
1031 (let ((url (get-text-property (point) 'shr-url)))
1032 (if (not url)
1033 (message "No URL under point")
1034 (url-retrieve url 'eww-download-callback (list url)))))
1035
1036 (defun eww-download-callback (status url)
1037 (unless (plist-get status :error)
1038 (let* ((obj (url-generic-parse-url url))
1039 (path (car (url-path-and-query obj)))
1040 (file (eww-make-unique-file-name (file-name-nondirectory path)
1041 eww-download-path)))
1042 (write-file file)
1043 (message "Saved %s" file))))
1044
1045 (defun eww-make-unique-file-name (file directory)
1046 (cond
1047 ((zerop (length file))
1048 (setq file "!"))
1049 ((string-match "\\`[.]" file)
1050 (setq file (concat "!" file))))
1051 (let ((count 1))
1052 (while (file-exists-p (expand-file-name file directory))
1053 (setq file
1054 (if (string-match "\\`\\(.*\\)\\([.][^.]+\\)" file)
1055 (format "%s(%d)%s" (match-string 1 file)
1056 count (match-string 2 file))
1057 (format "%s(%d)" file count)))
1058 (setq count (1+ count)))
1059 (expand-file-name file directory)))
1060
1061 ;;; Bookmarks code
1062
1063 (defvar eww-bookmarks nil)
1064
1065 (defun eww-add-bookmark ()
1066 "Add the current page to the bookmarks."
1067 (interactive)
1068 (eww-read-bookmarks)
1069 (dolist (bookmark eww-bookmarks)
1070 (when (equal eww-current-url
1071 (plist-get bookmark :url))
1072 (error "Already bookmarked")))
1073 (if (y-or-n-p "bookmark this page? ")
1074 (progn
1075 (let ((title (replace-regexp-in-string "[\n\t\r]" " " eww-current-title)))
1076 (setq title (replace-regexp-in-string "\\` +\\| +\\'" "" title))
1077 (push (list :url eww-current-url
1078 :title title
1079 :time (current-time-string))
1080 eww-bookmarks))
1081 (eww-write-bookmarks)
1082 (message "Bookmarked %s (%s)" eww-current-url eww-current-title))))
1083
1084 (defun eww-write-bookmarks ()
1085 (with-temp-file (expand-file-name "eww-bookmarks" user-emacs-directory)
1086 (insert ";; Auto-generated file; don't edit\n")
1087 (pp eww-bookmarks (current-buffer))))
1088
1089 (defun eww-read-bookmarks ()
1090 (let ((file (expand-file-name "eww-bookmarks" user-emacs-directory)))
1091 (setq eww-bookmarks
1092 (unless (zerop (or (nth 7 (file-attributes file)) 0))
1093 (with-temp-buffer
1094 (insert-file-contents file)
1095 (read (current-buffer)))))))
1096
1097 (defun eww-list-bookmarks ()
1098 "Display the bookmarks."
1099 (interactive)
1100 (eww-bookmark-prepare)
1101 (pop-to-buffer "*eww bookmarks*"))
1102
1103 (defun eww-bookmark-prepare ()
1104 (eww-read-bookmarks)
1105 (when (null eww-bookmarks)
1106 (error "No bookmarks are defined"))
1107 (set-buffer (get-buffer-create "*eww bookmarks*"))
1108 (eww-bookmark-mode)
1109 (let ((format "%-40s %s")
1110 (inhibit-read-only t)
1111 start url)
1112 (erase-buffer)
1113 (setq header-line-format (concat " " (format format "URL" "Title")))
1114 (dolist (bookmark eww-bookmarks)
1115 (setq start (point))
1116 (setq url (plist-get bookmark :url))
1117 (when (> (length url) 40)
1118 (setq url (substring url 0 40)))
1119 (insert (format format url
1120 (plist-get bookmark :title))
1121 "\n")
1122 (put-text-property start (1+ start) 'eww-bookmark bookmark))
1123 (goto-char (point-min))))
1124
1125 (defvar eww-bookmark-kill-ring nil)
1126
1127 (defun eww-bookmark-kill ()
1128 "Kill the current bookmark."
1129 (interactive)
1130 (let* ((start (line-beginning-position))
1131 (bookmark (get-text-property start 'eww-bookmark))
1132 (inhibit-read-only t))
1133 (unless bookmark
1134 (error "No bookmark on the current line"))
1135 (forward-line 1)
1136 (push (buffer-substring start (point)) eww-bookmark-kill-ring)
1137 (delete-region start (point))
1138 (setq eww-bookmarks (delq bookmark eww-bookmarks))
1139 (eww-write-bookmarks)))
1140
1141 (defun eww-bookmark-yank ()
1142 "Yank a previously killed bookmark to the current line."
1143 (interactive)
1144 (unless eww-bookmark-kill-ring
1145 (error "No previously killed bookmark"))
1146 (beginning-of-line)
1147 (let ((inhibit-read-only t)
1148 (start (point))
1149 bookmark)
1150 (insert (pop eww-bookmark-kill-ring))
1151 (setq bookmark (get-text-property start 'eww-bookmark))
1152 (if (= start (point-min))
1153 (push bookmark eww-bookmarks)
1154 (let ((line (count-lines start (point))))
1155 (setcdr (nthcdr (1- line) eww-bookmarks)
1156 (cons bookmark (nthcdr line eww-bookmarks)))))
1157 (eww-write-bookmarks)))
1158
1159 (defun eww-bookmark-quit ()
1160 "Kill the current buffer."
1161 (interactive)
1162 (kill-buffer (current-buffer)))
1163
1164 (defun eww-bookmark-browse ()
1165 "Browse the bookmark under point in eww."
1166 (interactive)
1167 (let ((bookmark (get-text-property (line-beginning-position) 'eww-bookmark)))
1168 (unless bookmark
1169 (error "No bookmark on the current line"))
1170 ;; We wish to leave this window, but if it's the only window here,
1171 ;; just let it remain.
1172 (ignore-errors
1173 (delete-window))
1174 (eww-browse-url (plist-get bookmark :url))))
1175
1176 (defun eww-next-bookmark ()
1177 "Go to the next bookmark in the list."
1178 (interactive)
1179 (let ((first nil)
1180 bookmark)
1181 (unless (get-buffer "*eww bookmarks*")
1182 (setq first t)
1183 (eww-bookmark-prepare))
1184 (with-current-buffer (get-buffer "*eww bookmarks*")
1185 (when (and (not first)
1186 (not (eobp)))
1187 (forward-line 1))
1188 (setq bookmark (get-text-property (line-beginning-position)
1189 'eww-bookmark))
1190 (unless bookmark
1191 (error "No next bookmark")))
1192 (eww-browse-url (plist-get bookmark :url))))
1193
1194 (defun eww-previous-bookmark ()
1195 "Go to the previous bookmark in the list."
1196 (interactive)
1197 (let ((first nil)
1198 bookmark)
1199 (unless (get-buffer "*eww bookmarks*")
1200 (setq first t)
1201 (eww-bookmark-prepare))
1202 (with-current-buffer (get-buffer "*eww bookmarks*")
1203 (if first
1204 (goto-char (point-max))
1205 (beginning-of-line))
1206 ;; On the final line.
1207 (when (eolp)
1208 (forward-line -1))
1209 (if (bobp)
1210 (error "No previous bookmark")
1211 (forward-line -1))
1212 (setq bookmark (get-text-property (line-beginning-position)
1213 'eww-bookmark)))
1214 (eww-browse-url (plist-get bookmark :url))))
1215
1216 (defvar eww-bookmark-mode-map
1217 (let ((map (make-sparse-keymap)))
1218 (suppress-keymap map)
1219 (define-key map "q" 'eww-bookmark-quit)
1220 (define-key map [(control k)] 'eww-bookmark-kill)
1221 (define-key map [(control y)] 'eww-bookmark-yank)
1222 (define-key map "\r" 'eww-bookmark-browse)
1223 map))
1224
1225 (define-derived-mode eww-bookmark-mode nil "eww bookmarks"
1226 "Mode for listing bookmarks.
1227
1228 \\{eww-bookmark-mode-map}"
1229 (buffer-disable-undo)
1230 (setq buffer-read-only t
1231 truncate-lines t))
1232
1233 (provide 'eww)
1234
1235 ;;; eww.el ends here