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