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